【发布时间】:2020-04-26 08:00:01
【问题描述】:
我的脚本应该加载凭据和具有提升的运行代码。 我尝试将一个脚本块包含到另一个脚本块中。并使用不同的参数集运行 Start-Process。 我有错误“由于错误而无法运行此命令:存根收到错误数据”。
$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 }"```
【问题讨论】:
标签: powershell credentials runas scriptblock