小编Sea*_*ean的帖子

Powershell EncodedCommand长度问题

我正在尝试使用powershell.exe的-EncodedCommand参数以不同的用户身份运行powershell脚本.我这样做是为了避免处理转义引号和其他特殊字符的命令行困难.我发现当编码命令的长度超过916个字符时,它会失败并显示以下消息:

Start-Process : This command cannot be run due to the error: The stub received bad data.
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

$path = 'c:\temp'

$UserName = '.\someuser'
$Password = 'somepassword'
$securePassword = ($Password | ConvertTo-SecureString -AsPlainText -Force)
$credential = New-Object System.Management.Automation.PSCredential $UserName, $securePassword

$command = {& .\Restore-DatabaseFromBackupFile.ps1 -DatabaseName 'aaaaaaaaaaaaaaa' -DatabaseBackupFilePath 'aaaaaaaaaaaaaaaaaaaaaaaaaaa' -DatabaseDataLogicalName 'aaaaaaaaaaaaaaa' -DatabaseLogLogicalName 'aaaaaaaaaaaaaaaaaaa' -DatabaseDataFilePath 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef' -DatabaseLogFilePath 'a';Start-Sleep -Seconds 2}
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($commandBytes)
Write-Warning $encodedCommand.Length
Write-Warning $encodedCommand
Start-Process -FilePath 'powershell.exe' -ArgumentList "-ExecutionPolicy Unrestricted -EncodedCommand $encodedCommand" -WorkingDirectory $path -LoadUserProfile -Credential …
Run Code Online (Sandbox Code Playgroud)

powershell

7
推荐指数
1
解决办法
1492
查看次数

标签 统计

powershell ×1