最初发布到堆栈溢出,并被指示在这里。 我是新手编程,正在尝试编写一个PowerShell脚本来pipe理软件更新。 我在一个拥有50多个远程办公室的Windows域环境中。 每个办公室有4台电脑。 一台计算机“计算机A”安装了Dropbox,用于备份和现在的软件更新。 我想从本地机器上执行一个脚本,将一个文件夹(〜1 GB)复制到该局域网中的其他计算机上,然后运行目录中的setup.exe文件来更新这些计算机上的软件。
据我所知,为了使用Enter-pssession cmdlet并运行复制项目和启动过程,我必须:
从这里我可以进入Power shell会话并开始复制和安装。 这是我正在运行的代码:
#Allow my computer to send credentials to remote computers Enable-WSManCredSSP -Role Client -DelegateComputer * -Force $credential = Get-Credential -Credential domain\user #Getting Content $path = "C:\computernames.csv" Import-Csv -path $path | foreach-object { $computername = $_.Name if (Test-Connection -cn $_.Name -quiet) { #copy the batch file that allows remote powershell access $source = "c:\" $destination = "\\$computername\c$\install files\remoteaccess" robocopy $source $destination #open file to allow remote PS access & psexec \\$computername -a runas -u domain\user -p password -c -f -h "\\$computername\c$\install files\remoteaccess\remoteaccess.bat" #Initiates Power Shell session with remote computer Enter-PSSession -cn $_.Name -Credential $credential -Authentication Credssp #begin copy #be sure to refine destination copy-item -path $_.Source -Destination $_.Destination -force -verbose -recurse | out-file c:\copylog.txt #begin silent install #code here #stop wsmancredssp accep disable-wsmancredssp -role server #close pssession exit-pssession } else { "$computername is not online" } } Disable-wsmancredssp -role client
computernames.csv具有标题:名称,源,具有UNC目录的目标,每个局域网的计算机都带有保pipe箱以及复制文件的位置。
“remoteaccess.bat”文件包含以下两行代码:
powershell.exe enable-psremoting -force >>c:\remotelog.txt powershell.exe Enable-WSManCredSSP -role Server -force >>c:\remotelog.txt
启动与远程计算机的psexec会话后,powershell窗口会冻结,并发送第一个powershell命令以启用远程处理。 没有错误产生,或input请求。 该命令的输出保存在remotelog文本文件中:“WinRM已经更新以接收请求,WinRM服务启动。
WinRM已经被设置为在这台机器上进行远程pipe理。“
看来powershell命令启用wsmancredssp是永远不会收到的。 当我从远程访问软件运行远程计算机上的batch file时,这两个命令执行并成功logging。 我的psexec命令出错了,或者如何设置这些计算机以允许使用远程命令而不使用GPO进行远程会话?
代码closures的方式有很多种: