使用Invoke-Command在PSSession中运行Powershell脚本

时间:2019-12-13 01:38:13

标签: powershell copy credentials invoke-command copy-item

因此,我有一个“父”脚本,该脚本对某些Web服务器执行了一个invoke-命令,而在该invoke-command的内部,我有一个“ Child”脚本,该脚本正在删除和复制一些文件。

这是父脚本: (注意:$ cred变量现在是通过securestring经过验证且可以正常运行的域管理员)

foreach ($server in $servers) {
    invoke-command -ComputerName $server -Credential $cred -ScriptBlock {
    & 'D:\Web\Deploy_CopyStaging2AppPools.ps1'
    }

无论出于何种原因,子脚本似乎都出现了权限错误(我认为?)

子脚本看起来像这样

$AppPoolDirs = @(
"SisterSitesCategory",
"SisterSitesManufacturer",
"TestimStopTrunk",
"1ss_bot2",
"1ss_test",
"1sspro",
"Canadabot2",
"DevelopBot2",
"hcbot2",
"LDEBOT2",
"LnBOT2"
)
#timestamp filter
filter timestamp {"$(Get-Date -Format o): $_"}
Write-Output "Starting AppPool Filecopies on $env:COMPUTERNAME" | Timestamp | Out-File -FilePath "D:\Web\Deploylog.txt" -Append

Foreach ($AppPoolDir in $AppPoolDirs) {
    Write-Output "The working AppPoolDirectory is $AppPoolDir"
    if
    (test-path D:\Web\AppPools\$AppPoolDir) {
        Write-Output "######APP POOL DIRECTORY FOUND#######! Gonna do stuff to the AppPoolDir $AppPoolDir"
        remove-item -path "D:\Web\AppPools\$AppPoolDir\bin\App_Web_*.dll" -WhatIf
        copy-item -path "\\1sl-den-web01\d$\web\staginglive\staging\*" -Destination "D:\Web\AppPools\$AppPoolDir" -recurse -Force -WhatIf
        }
    else {
        Write-Output "Directory $env:COMPUTERNAME D:\Web\AppPools\$AppPoolDir Doesn't exist, no removal or copying was done"
        }
    }

remove-item有效,而copy-item不起作用。由于copy-item -path位于远程计算机上,因此我猜测这是权限问题。

如果这是权限问题,那么我猜“父”脚本在以适当凭据打开invoke-命令时,不是在以适当凭据调用子脚本吗?

0 个答案:

没有答案