让我首先说我已经浏览了在线资源,无法弄清楚如何通过我的powershell脚本正确传递管理员凭据(用户和密码)。我有一个脚本,我们称之为script.ps1,脚本看起来像这样(我有PowerShell v1.0):
$UserName = "domain\user"
$Password = ConvertTo-SecureString -String "PASSWORD" -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $UserName, $Password
$events = Get-WinEvent -Credential $Credentials -ComputerName thecomputername -LogName Microsoft-Windows-PrintService/Operational
$resultArry = @()
foreach($ev in $events)
{
Try
{
$evXML = [xml]$ev.ToXml()
if( $ev.Id -eq 307 )
{
$obj = New-Object PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Name "User" -Value $evXML.Event.UserData.DocumentPrinted.Param3
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Document" -Value $evXML.Event.UserData.DocumentPrinted.Param2
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Computer" -Value $evXML.Event.System.Computer
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Event Timestamp" -Value $evXML.Event.System.TimeCreated.GetAttribute("SystemTime")
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Printer" -Value $evXML.Event.UserData.DocumentPrinted.Param5
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Port" -Value $evXML.Event.UserData.DocumentPrinted.Param6
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Pages" -Value $evXML.Event.UserData.DocumentPrinted.Param8
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Size in Bytes" -Value $evXML.Event.UserData.DocumentPrinted.Param7
$script:resultArry += $obj
}
}
Catch
{
}
}
$resultArry | Export-CSV C:\mycsv.csv -NoTypeInformation
pause
当我双击此脚本运行它时,我仍然获得权限被拒绝。我希望自动输入管理员帐户,以便脚本可以像这样运行。如果我摆脱了顶部的代码,右键单击我的文件并以管理员身份运行并手动输入我的凭据,它将成功运行。我显然没有正确使用/传递凭证,所以我希望有人能看到我做错了什么?
我不想设置图标快捷方式,以便在点击时自动以管理员身份运行,因为我有一个自动流程运行此脚本,并且该解决方法在这种情况下不起作用。我需要实际代码来传递凭据
感谢您的任何意见,非常感谢