Authenticating with PowerShell to Kill Process - powershell

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 E.g. 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:
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.

Related

Remove File on SFTP using Powershell

I'm struggling with the below code. Want to delete a file that is on SFTP. Unable to understand how to accomplish this -
$UserName = 'test'
$SecurePassword = ConvertTo-SecureString -String '3ea5e#9dkdadfsfwC' -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword
$Session = New-SFTPSession -ComputerName 'sftp.test.com' -Credential $Cred
$result = Remove-Item -LiteralPath "\\?sftp://test#sftp.test.com/export/home/dmsmaster/stms/PF/Working_Titles_Primary.csv" -Force
Used the following code to make it work -
$result = Remove-SFTPItem -path "/export/home/dmsmaster/stms/PF/Working_Titles_Primary.csv" -SFTPSession $Session -Force

encrypt username in PowerShell

Need to encrypt username and password in PowerShell I've encrypted password using the below code.
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString |
Set-Content E:\powershell\encrypted_password1.txt
But when I tried to encrypt username with the same code
$credential.username | ConvertFrom-SecureString |
Set-Content E:\powershell\encrypted_user1.txt
Encountered with below error:
ConvertFrom-SecureString : The input object cannot be bound to any parameters
for the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline
input.
Is there any way to encrypt username?
Exporting:
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content E:\powershell\encrypted_password1.txt
$credential.Username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Set-Content E:\powershell\encrypted_user1.txt
Importing:
$Username = Get-Content E:\powershell\encrypted_user1.txt | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Username)
$Username = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$Password = Get-Content E:\powershell\encrypted_password1.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName PSCredential -ArgumentList $Username, $Password

Issues passing credentials to a function Azure Powershell

Hi all I'm trying to pass server login credentials to my 'createSQLServer function but keep hitting the error 'Cannot process argument transformation on parameter 'creds'.userName'' I've tried a lot of different, even tried with a 'param block' but stull stuck. A push in the right direction would be appreciated, cheers.
###### SQL SERVER LOGIN CREDENTIALS
$userName = "aaron"
$password = "Password_1234"
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword
### SQL server names with resource group name appeneded
$server1 = "sqlserver1" + $resGroup
$serVerion = "12.0"
function createSQLserver ([string]$server1,[string]$server2, [string]$server3, [System.Management.Automation.PSCredential]$creds,[string]$server1Location, [string]$server2Location, [string]$server3Location, [string]$resGroup, [string]$serVerion, [string]$userName, [string]$password, [SecureString]$securePassword)
{
#Create server 1(Server A)
<#check to see if server exists - It exists, $continue is created and passed to
if statement to append two random characters to name#>
Write-Host "Creating First SQL Server"
$sqlServer = New-AzureRmSqlServer -ServerName $server1 -SqlAdministratorCredentials $creds -Location $server1Location -ResourceGroupName $resGroup -ServerVersion $serVerion -ErrorVariable continue -ErrorAction SilentlyContinue
if ($continue)
{
do {
$server1 = $server1 + (rand)
$sqlServer = New-AzureRmSqlServer -ServerName $server1 `
-SqlAdministratorCredentials $creds -Location $server1Location `
-ResourceGroupName $resGroup -ServerVersion "12.0" -ErrorVariable continue -ErrorAction SilentlyContinue
}
until(!$continue)
Write-Host 'exists creating new' $server1 'Created'
}else{
Write-Host $server1 ' Created'
}
Start-Sleep -s 2
}
createSQLserver $server1 $username $password $securePassword $creds $server1Location $resGroup $serVerion
You need to use your named parameters!
Here's a snippet of your first few parameters:
...
[string]$server1
,
[string]$server2
,
[string]$server3
,
[System.Management.Automation.PSCredential]$creds
...
And then the ones you're passing in to the function call
createSQLserver $server1 $username $password $securePassword ...
So because you're not using the names of your parameters, they are using their relative ordinal position i.e.
param | value
---------+----------------
$server1 | $server1
$server2 | $username
$server3 | $password
$creds | $securePassword
So what have we learned?
Always use named parameters!
createSQLserver -server1 $server1 -username $username -password $password -securePassword $securePassword
That should sort you out :-)

How to rename a domain computer in Powershell

I've been trying to rename a domain computer with the following script:
$username = "domain\username"
$password = "password"
$ip = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
$hostname = (nslookup $ip)[3]
$hostname = $hostname.replace(" ", "")
$hostname = $hostname.split(":")[1]
$hostname = $hostname.split(".")[0].ToLower()
Rename-Computer -NewName $hostname -DomainCredential $username -Restart -Force
It does everything I desire apart from inputting the password which at this point is a manual process. Can someone advise me on how to get it to input from $password into the prompt box so that I can completely automate the process?
Alternatively, if there's a better way to do it in Powershell, I'm open to going another direction.
You can use this code:
$Username = "DomainUserName"
$Password = "PlainPassword" | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($Username ,$Password)
Rename-Computer -NewName $newComputerName -ComputerName $OldName -Restart -DomainCredential $Creds

Getting Remote Process Information with Powershell (w/o remoting)

We have a bunch of PC that are not part of the domain (and cannot be added). They do not have PS installed and we'd prefer not to have to install it.
I want to use Powershell from a server to get the memory usage of 2 process every hour. Unfortunately get-process doesn't seem to support a -credential parameter. I did get win32_process (as shown below), but it returns a ton of info (no idea how I'd just get VMsize for two processes).
$Pass = ConvertTo-SecureString -string "SECRET" -AsPlainText –Force
$User = "USER"
$Cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pass
gwmi win32_process -computername PCName -Credential $Cred
Is there a way to do this without installing PS or putting PC's in domain?
You can use the Filter parameter to limit the processes you get info on e.g.:
Get-WmiObject -cn $c win32_process -Filter "Name='PowerShell.exe'" | ft Name, PrivatePageCount
Figured it out. This gets size of VM and Working set for the apps listed in $Processnames on the Computers listed in $HostNames. It checks if the computer is alive first
$Pass = ConvertTo-SecureString -string "SECRET" -AsPlainText –Force
$User = "User"
$Cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pass
$ProcessNames = #('App1.exe', 'App2.exe')
$HostList =#('Computer1','Computer2')
foreach ($CurrHost in $HostList)
{
# check if it's alive
if((Test-Connection -Cn $CurrHost -BufferSize 16 -Count 1 -ea 0 -quiet))
{
gwmi win32_process -computername $CurrHost -Credential $Cred |
Where-Object {$ProcessNames -contains $_.Name } |
Select-Object CSName, Name, WorkingSetSize, VirtualSize #|
#Format-Table -AutoSize
}
}