Reputation: 156
My script should load credentials an run code with elevation. I try to include one scriptblock to another. And run Start-Process with different parameter sets. I have error "This command cannot be run due to the error: The stub received bad data".
$ScriptBlock = {
Param(
[datetime] $Start = '04/25/2020 19:52:25',
[datetime] $End = '04/25/2020 19:52:35',
[string] $OutputXMLPath = 'C:\DATA\Projects\EventLogsAnalyzer\DATA\ScriptBlockOutput.xml',
[string] $logFilePath = 'C:\DATA\Projects\EventLogsAnalyzer\LOGS\EventLogsAnalyzer.log'
)
$ScriptBlock = {
Param(
[datetime] $Start = '04/25/2020 19:52:25',
[datetime] $End = '04/25/2020 19:52:35',
[string] $OutputXMLPath = 'C:\DATA\Projects\EventLogsAnalyzer\DATA\ScriptBlockOutput.xml'
)
$Logs = Get-WinEvent -ListLog *
$Res = @()
Foreach($Log in $Logs) {
$Log.LogName
$Filter = @{
LogName = $Log.LogName
StartTime = $Start
EndTime = $End
}
$Res += Get-WinEvent -FilterHashTable $Filter -ErrorAction SilentlyContinue
}
}
$Res = Start-Process -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PassThru -Verb "RunAs" -ArgumentList " -NoExit -ExecutionPolicy Bypass –NoProfile -Command & { $ScriptBlock }"
$Res
}
$User = Get-VarFromAESFile $Global:GlobalKey1 $Global:APP_SCRIPT_ADMIN_Login
$Pass = Get-VarFromAESFile $Global:GlobalKey1 $Global:APP_SCRIPT_ADMIN_Pass
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList (Get-VarToString $User), $Pass
Start-Process -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PassThru -Credential $Credentials -ArgumentList " -NoExit -ExecutionPolicy Bypass –NoProfile -Command & { $ScriptBlock }"```
Upvotes: 0
Views: 325
Reputation: 16106
A similar discussion on Stackoverflow can be seen here:
A comment with thinking about in the Q&A
Any reason you're not using Invoke-Command which can run a script block locally or remotely with specified credentials (see example 2 in its help)?
See also as an association with the aforementioned:
The accepted answer:
Here is something that is working:
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `$outvar1`"}`"" -Wait
-ArgumentList is an array of strings $outvar is interpreted so I use `$outvar
Upvotes: 0