简体   繁体   中英

Authenticating with PowerShell to Kill Process

I would like to be able to run this from different workstations which mean we need to authenticate. I have this below but it doesn't seem to work. Can you see anything wrong.

get-content .\killprocess.PS1

$username = 'je\leigh'
$password = cat 'U:\Accounts\Synergy Tools\securestring.txt' | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `-argumentlist $username, $password

(Get-Content 'U:\Accounts\Synergy Tools\Computers.txt') | ForEach-Object {Get-WmiObject -computer $_ -class win32_process -filter "name='synergy.exe' or name='booking9.exe' or name='acl_apps.exe' or name='pos.exe' or name='booking.exe'" -credential $cred| %{$_.terminate()} | out-null}

If securestring.txt contains a readable password Eg MyP@ssWord!123 . Try using ConvertTo-SecureString 's -AsPlainText & -Force parameters:

$password = Get-Content 'U:\...\securestring.txt' | ConvertTo-SecureString -AsPlainText -Force

This will convert the text in securestring.txt into a readable SecureString , which can be viewed with $Password | ConvertFrom-SecureString $Password | ConvertFrom-SecureString :

01000000d08c9ddf0115d1118c7a00c04fc297eb010000000a65b1b20fe4e0418aece793d7242358
0000000002000000000003660000c00000001000000082e9a55adb7c90d9bed5946aed69e5380000
000004800000a000000010000000932bccf24879272d36536f6ef98e1be4180000006fb092d673a8
1f84b5954f9cb35daee2addb2d325ec25112140000005c8982b4cd15503000796213c3e0d3869780

Then just give the $password variable straight to -argumentlist $username, $password

If you wanted to you could store the output of ConvertFrom-SecureString into the securestring.txt then use the following in your script:

$password = Get-Content 'U:\...\securestring.txt' | ConvertTo-SecureString     

Note : You may get errors if there are more than one line in securestring.txt as it will turn $password an array of strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM