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

远程计算机上的Powershell激活脚本-远程计算机无法查看网络共享,powershell,remote-server,Powershell,Remote Server,我有一个数据库服务器不在域中,任务计划程序已损坏,我还没有时间或资源修复它,因此我创建了一个脚本,使用任务计划程序从另一个非域服务器运行,该脚本激活数据库服务器上的第二个脚本以复制文件。在数据库服务器上,当手动激活脚本时,它会查看所有内容并执行其工作,但当我尝试远程激活脚本时,它会运行并查看除网络驱动器以外的所有内容(W:)。我在脚本中使用了credssp,该脚本连接到远程服务器,请参见下文-问题:如何获取脚本以查看远程服务器上的网络共享 -------------脚本A------------

我有一个数据库服务器不在域中,任务计划程序已损坏,我还没有时间或资源修复它,因此我创建了一个脚本,使用任务计划程序从另一个非域服务器运行,该脚本激活数据库服务器上的第二个脚本以复制文件。在数据库服务器上,当手动激活脚本时,它会查看所有内容并执行其工作,但当我尝试远程激活脚本时,它会运行并查看除网络驱动器以外的所有内容(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
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
$username=“管理员”
$computerA=“”
$computerB=“”
$PwdLocation=“c:\test\password.txt”
#启用wsmancredssp-角色客户端-委派计算机$computerB
$password=获取内容$PwdLocation |转换为SecureString
$credential=新对象-typename System.Management.Automation.PSCredential-argumentlist$username,$password
调用命令-ComputerName$computerB-ScriptBlock{\\\测试\删除.ps1}-ArgumentList$ComputerA-Credential$Credential
出口
----------------------远程脚本B---------------------------------------

$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
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
函数邮件器{
$recipients=“”
$smtp=“smtp.org”
$emailTo=$recipients
$emailFrom=“否-reply@org.org"
$smtpserver=“smtp.org”
$smtp=新对象Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom、$emailTo、$subject、$message)
}
函数停止一切{
$subject=“由于没有连接而停止脚本Delete.PS1”
$message=“”
对于($i=0;$i-le$tot-1;$i++)
{
$path=$bfs[$i]
if(存在目录($path))
{ 
$message+=[string]::concat($path,“Connected`n”)
} 
其他的
{ 
$message+=[string]::concat($path,“cannotconnect`n”)
} 
}
梅勒
出口
}
函数存在Dir($path){
如果([IO.Directory]::存在($path))
{ 
返回$true;
} 
其他的
{ 
返回$false;
} 
} 
$ScriptStart=(获取日期)
[array]$bfs=“F:\Backups\NetPerfMon”,“F:\Backups\NetPerfMon\u Aux”,“W:\Backups\NetPerfMon\u Aux”,“W:\Backups\NetPerfMon”
$tot=$bfs.count
对于($i=0;$i-le$tot-1;$i++)
{
$path=$bfs[$i]
if(存在目录($path))
{ 
$message+=[string]::concat($path,“Connected`n”)
“+$ScriptStart”上的$subject=“Start Script Delete.PS1
} 
其他的
{ 
$message+=[string]::concat($path,“cannotconnect`n”)
停止一切
} 
}
$message+=“”
$message+=
梅勒
$limit=(获取日期).AddDays(-7)
$limit1=(获取日期).AddDays(-14)
$limit2=(获取日期).AddDays(-1)
$FB=“F:\Backups\NetPerfMon”
$FBAux=“F:\Backups\NetPerfMon\u Aux”
$WBAux=“W:\Backups\NetPerfMon\u Aux”
$WBBak=“W:\Backups\NetPerfMon”
Get ChildItem-Path$FB | Where Object{!$\u.PSIsContainer-和$\u.LastWriteTime-lt$limit}|删除项-Force | OUT-NULL |删除大于7天的项
Get ChildItem-Path$FBAux | Where Object{!$\u.PSIsContainer-和$\u.LastWriteTime-lt$limit}|移除项-Force | OUT-NULL |移除大于7天的项
Get ChildItem-Path$WBBak | Where对象{!$\u.PSIsContainer-和$\u.LastWriteTime-lt$limit1}|移除项目-Force | OUT-NULL |移除超过14天的项目
获取ChildItem-Path$FB | where{$$.extension-eq.bak“}where Object{!$\ psicContainer-和$\ LastWriteTime-gt$limit2}复制项-destination$WBBak | OUT-NULL#在1天内复制具有extension.bak的项
获取ChildItem-Path$FBAux | where{$$$.extension-eq.bak”}where Object{!$\.psicContainer-和$\.LastWriteTime-gt$limit2}复制项-destination$WBAux | OUT-NULL#在1天内复制具有extension.bak的项
$ScriptEnd=(获取日期)
$RunTime=New Timespan-Start$ScriptStart-End$ScriptEnd
“运行时间:{0}:{1}:{2}”-f$RunTime.Hours、$RunTime.Minutes、$RunTime.Seconds
$subject=“在”+$ScriptEnd上停止脚本删除.PS1
$message=“”
$message+=“”
$message+=“完成时间:{0}:{1}:{2}”-f$RunTime.Hours、$RunTime.Minutes、$RunTime.Seconds
梅勒

使用完整的UNC路径,而不是驱动器号,或者在脚本顶部执行“net Use X:\my\UNC\share”,将驱动器映射到脚本中


编辑:如果执行此操作,则必须在net use命令中明确指定密码:
net use x:\\my\share/user:mydom\myuser mypassword

是否与其他没有驱动器W:\的用户一起使用此脚本?您可以使用UCN路径(\\server\share)而不是W:\?如Patrick所说,如果credssp实际工作,您应该能够使用直接的UNC路径。在远程ps会话中运行的脚本中,将驱动器映射为“net use”,然后复制到该驱动器号,这是一种常见的、低级的黑客行为,但credssp不需要这样做。如果委托正在工作,则在使用UNC路径时可以避免“双跳”错误。另外,仅仅修复任务调度程序引擎是否真的比使用这个小技巧进行futz操作要费劲得多?