远程计算机上的Powershell激活脚本 - 远程计算机无法查看网络共享

时间:2014-08-27 17:54:12

标签: powershell remote-server

我有一个不在域中的数据库服务器,任务调度程序已损坏,我没有时间或资源来修复它所以我创建了一个脚本,该脚本使用任务调度程序从另一个非域服务器运行激活位于数据库服务器上的第二个脚本以复制文件。在数据库服务器上,手动激活时脚本会查看所有内容并完成其工作,但是当我尝试远程激活脚本时,它会运行并查看除网络驱动器之外的所有内容(W :)。我在脚本连接中使用credssp,请参阅下面的内容 - 请问如何让脚本在远程服务器上看到网络共享

-------------脚本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 = "no-reply@org.org"
$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

1 个答案:

答案 0 :(得分:0)

使用完整的UNC路径而不是驱动器字母或在脚本顶部执行“net use X:\ my \ unc \ share”,在脚本中映射驱动器。

编辑:如果您这样做,则必须在net use命令中明确指定密码:net use x: \\my\share /user:mydom\myuser mypassword

相关问题