从rundecklinux运行Powershell显示不同的结果

问题描述

我正在尝试从rundeck(linux)运行powershell脚本,如果我在本地运行脚本[从多个终端服务器删除某些文件](Windows服务器),则按预期运行,但是如果我从rundeck服务器调用它(已配置Winrm),该脚本似乎无法访问我尝试访问的远程文件夹。

我尝试使用同一用户运行脚本,但仍然显示不同的结果。

下面的脚本:

$userAD = "someuser"
$servers = Get-Content C:\TSList.csv
$Folder = "c$\Users\$userAD\"
$TSFolderShare = "\\sharepath"

Write-Output "#####Start of script#####"
Write-output `n
Write-output "Checking if $userAD user profile exist in Terminal servers..."

sleep -seconds 1

foreach ($server in $servers) {

Test-Path "\\$server\$Folder" -PathType Any
Get-ChildItem "\\$server\$Folder" 

    if (Test-Path "\\$server\$Folder" -PathType Any) {
        Write-output  "Resetting user profile in $server.."
                   Get-ChildItem "\\$server\$Folder" -Recurse -Force -ErrorAction SilentlyContinue  | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue 
        
        sleep -seconds 1
        Write-output "Done." 

        if( (Get-ChildItem "\\$server\$Folder" | Measure-Object).Count -eq 0)
             {
                Write-output "Done." 
             }
        
    }
    else
    {
        Write-output  "Resetting user profile in $server.."
        sleep -seconds 1
        Write-output  "User profile does not exist in $server."          
        #Write-output "\\$server\$Folder does not exist in $server!" -ForegroundColor Red
    }

}

编辑:看来我的问题是使用RunAS从另一个脚本运行我的脚本时。

下面,我尝试使用ps脚本从另一台服务器访问文件夹,但是由于我要将其集成到Rundeck,因此我需要使用python从我的Linux服务器中调用ps脚本。我进行了直接运行ps脚本的测试,并使用与我手动运行脚本的用户相同的用户使用RunUs调用另一个脚本来调用测试路径脚本

  1. 场景1 通过带有RunAS(my_account)的单独PS脚本运行PS脚本

    $ username =“ my_account” $ password =“ my_password” $ secstr =新对象-TypeName System.Security.SecureString $ password.ToCharArray()| ForEach对象{$ secstr.AppendChar($ _)} $ cred =新对象-typename System.Management.Automation.PSCredential -argumentlist $ username,$ secstr

    调用命令-FilePath“ C:\ testpath.ps1”-凭据$ cred-计算机本地主机

(C:\ testpath.ps1)以下内容:

Test-Path "\\server\c$\Users\myaccount\"

结果:

访问被拒绝 + CategoryInfo:PermissionDenied:(\ server \ c $ \ Users \ myaccount:String)[测试路径],UnauthorizedAccessException + FullyQualifiedErrorId:ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand + PSComputerName:本地主机

错误

  1. 场景2 直接作为my_account运行C:\ testpath.ps1

Test-Path "\\server\c$\Users\myaccount\"

结果: 是

解决方法

我在Powershell中使用了会话配置来解决此问题。通过这种方式,您可以将凭据绑定到PowerShell会话配置,并将此配置用于将来的所有连接。

https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp/

非常感谢!

,

您面临着Rundeck和Powershell的双重跳跃问题,here的解释。之前有人问过,看看thishere是一个很好的解决方法。也this来解决。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...