2014-08-27 160 views
0

我有一個數據庫服務器不在域中,該任務計劃程序已損壞,我沒有時間或資源來修復它,所以我創建了一個腳本,該腳本使用任務計劃程序從另一個非域服務器運行,該腳本激活位於數據庫服務器上的第二個腳本以複製文件。在數據庫服務器上,手動激活的腳本可以看到所有內容並執行其工作,但是當我嘗試遠程激活腳本時,它會運行並查看除網絡驅動器(W :)之外的所有內容。我正在使用連接好的腳本中的credssp見下面 - 問題如何讓腳本看到遠程服務器上的網絡共享遠程計算機上的Powershell激活腳本 - 遠程計算機無法看到網絡共享

-------------腳本A-- -------------------------------------------

$username = "Administrator" 
$computerA = "<addressA>" 
$computerB = "<addressB>" 
$PwdLocation = "c:\test\password.txt" 
#enable-wsmancredssp -role client -delegatecomputer $computerB 
$password = Get-Content $PwdLocation | ConvertTo-SecureString 
$credential = new-object -typename System.Management.Automation.PSCredential - argumentlist $username,$password 
Invoke-Command -ComputerName $computerB -ScriptBlock {\\<remote server>\test\delete.ps1} -ArgumentList $ComputerA -Credential $credential 
exit 

----------------------遠程腳本B ------------------------- --------------

Function mailer { 
$recipients = "<names>" 
$smtp = "smtp.org" 
$emailTo = $recipients 
$emailFrom = "[email protected]" 
$smtpserver="smtp.org" 
$smtp=New-Object Net.Mail.SmtpClient($smtpServer) 
$smtp.Send($emailFrom, $emailTo, $subject, $message) 
} 
Function StopEverything { 
$subject = "Stopped Script Delete.PS1 becuase of no connection" 
$message = "" 
for($i=0;$i -le $tot-1;$i++) 
{ 
$path = $bfs[$i] 
if (Exists-Dir($path)) 
{ 
$message += [string]::concat($path, " Connected`n") 
} 
else 
{ 
$message += [string]::concat($path, " Cannot Connect`n") 

} 
} 
mailer 
Exit 
} 

Function Exists-Dir($path) { 
    if ([IO.Directory]::Exists($path)) 
    { 
     return $true; 
    } 
    else 
    { 
     return $false; 
    } 
} 

$ScriptStart = (Get-Date) 
[array]$bfs = "F:\Backups\NetPerfMon","F:\Backups\NetPerfMon_Aux","W:\Backups\NetPerfMon_Aux","W:\Backups\NetPerfMon" 
$tot = $bfs.count 

for($i=0;$i -le $tot-1;$i++) 
{ 
$path = $bfs[$i] 
if (Exists-Dir($path)) 
{ 
$message += [string]::concat($path, " Connected`n") 
$subject = "Start Script Delete.PS1 on " + $ScriptStart 
} 
else 
{ 
$message += [string]::concat($path, " Cannot Connect`n") 
StopEverything 
} 
} 


$message += " " 
$message += 
mailer 
$limit = (Get-Date).AddDays(-7) 
$limit1 = (Get-Date).AddDays(-14) 
$limit2 = (Get-Date).AddDays(-1) 
$FB  = "F:\Backups\NetPerfMon" 
$FBAux = "F:\Backups\NetPerfMon_Aux" 
$WBAux = "W:\Backups\NetPerfMon_Aux" 
$WBBak = "W:\Backups\NetPerfMon" 

Get-ChildItem -Path $FB  | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -  lt $limit } | Remove-Item -Force | OUT-NULL #Remove items greater than 7 days 
Get-ChildItem -Path $FBAux | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Remove-Item -Force | OUT-NULL #Remove items greater than 7 days 
Get-ChildItem -Path $WBBak | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit1} | Remove-Item -Force | OUT-NULL #Remove items greater than 14 days 
Get-ChildItem -Path $FB  | where {$_.extension -eq ".bak"} | Where-Object {  !$_.PSIsContainer -and $_.LastWriteTime -gt $limit2} | Copy-Item -destination $WBBak | OUT- NULL #Copy items within 1 day that have extension .bak 
Get-ChildItem -Path $FBAux | where {$_.extension -eq ".bak"} | Where-Object {  !$_.PSIsContainer -and $_.LastWriteTime -gt $limit2} | Copy-Item -destination $WBAux | OUT- NULL #Copy items within 1 day that have extension .bak 
$ScriptEnd = (Get-Date) 
$RunTime = New-Timespan -Start $ScriptStart -End $ScriptEnd 
"Elapsed Time: {0}:{1}:{2}" -f $RunTime.Hours,$Runtime.Minutes,$RunTime.Seconds 
$subject = "Stop Script Delete.PS1 on " + $ScriptEnd 
$message = "" 
$message += " " 
$message += "Time to completion: {0}:{1}:{2}" -f  $RunTime.Hours,$Runtime.Minutes,$RunTime.Seconds 
mailer 
+0

你る這個劇本與沒有驅動器W上的其他用戶:\?你可以使用UCN路徑(\\ server \ share)而不是W:\? – Patrick 2014-08-27 18:17:04

+0

像帕特里克說,如果credssp實際上工作,你應該能夠使用直接的UNC路徑。在遠程ps會話中運行的腳本中,將一個驅動器的「net use」映射到映射中,然後複製到該驅動器盤符是一種常見的,俗套的破解方法,但是,credssp使得這不必要。如果委派工作正常,則避免使用UNC路徑時出現「雙跳」錯誤。另外,僅僅修復任務調度引擎比在這個小小的黑客中解決問題真的要多得多嗎? – red888 2014-08-27 20:44:31

回答

0

使用完整的UNC路徑不驅動器號或做一個「網絡使用X:\我的\ UNC \共享」在你的腳本的頂部在腳本中正確映射驅動器。

編輯:如果你這樣做,你必須明確指定net use命令密碼:net use x: \\my\share /user:mydom\myuser mypassword