Powershell: Difference between revisions

From Piszczynski
piszczynski>Aleks
piszczynski>Aleks
Line 3: Line 3:


Open another powershell window as admin:
Open another powershell window as admin:
<pre>
 
Start-Process powershell -Verb runAs
<span style='color:#666616; '>Start-Process</span> <span style='color:#0000e6; '>powershell</span> <span style='color:#074726; '>-Verb</span> <span style='color:#0000e6; '>runAs</span>
</pre>
 


Change to environment locations:
Change to environment locations:
<pre>
cd $Env:<vairable>
cd $Env:userprofile
</pre>


Restart computer remotely
<span style='color:#005fd2; '>cd</span> <span style='color:#797997; '>$</span><span style='color:#007997; '>Env:</span><span style='color:#0000e6; '>&lt;vairable></span>
<pre>
 
restart-computer -Computername [hostname] -Credential [domain\username] -force
<span style='color:#005fd2; '>cd</span> <span style='color:#797997; '>$</span><span style='color:#007997; '>Env:</span><span style='color:#797997; '>userprofile</span>
</pre>
 
 
Restart computer remotely:
 
<span style='color:#666616; '>restart-computer</span> <span style='color:#074726; '>-Computername</span> <span style='color:#0000e6; '>[hostname]</span> <span style='color:#074726; '>-Credential</span> <span style='color:#0000e6; '>[domain\username]</span> <span style='color:#074726; '>-force</span>
 


Connect to remote powershell session:
Connect to remote powershell session:
<pre>
 
$cred=Get-Credential
<span style='color:#797997; '>$cred</span><span style='color:#44aadd; '>=</span><span style='color:#666616; '>Get-Credential</span>
$sess = New-PSSession -Credential $cred -ComputerName <remotemachinename>
 
Enter-PSSession $sess
<span style='color:#797997; '>$sess</span> <span style='color:#44aadd; '>=</span> <span style='color:#666616; '>New-PSSession</span> <span style='color:#074726; '>-Credential</span> <span style='color:#797997; '>$cred</span> <span style='color:#074726; '>-ComputerName</span> <span style='color:#0000e6; '>&lt;remotemachinename></span>
<Run commands in remote session>
 
Exit-PSSession
<span style='color:#666616; '>Enter-PSSession</span> <span style='color:#797997; '>$sess</span>
Remove-PSSession $sess
 
</pre>
<span style='color:#005fd2; '>&lt;Run</span> <span style='color:#0000e6; '>commands</span> <span style='color:#0000e6; '>in</span> <span style='color:#0000e6; '>remote</span> <span style='color:#0000e6; '>session></span>
 
<span style='color:#666616; '>Exit-PSSession</span>
 
<span style='color:#666616; '>Remove-PSSession</span> <span style='color:#797997; '>$sess</span>
 


Get services running on computer and display in a pauseable list:
Get services running on computer and display in a pauseable list:
<pre>
 
Get-service | Where-Object {$_.Status -eq "Stopped"} | More
<span style='color:#666616; '>Get-service</span> <span style='color:#bb7977; font-weight:bold; '>|</span> <span style='color:#666616; '>Where-Object</span> <span style='color:#808030; '>{</span><span style='color:#007997; '>$_</span><span style='color:#44aadd; '>.</span><span style='color:#005fd2; '>Status</span> <span style='color:#44aadd; '>-eq</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Stopped</span><span style='color:#800000; '>"</span><span style='color:#808030; '>}</span> <span style='color:#bb7977; font-weight:bold; '>|</span> <span style='color:#005fd2; '>More</span>
gsv | where {$_.Status -eq "running"} | more
 
</pre>
<span style='color:#666616; '>gsv</span> <span style='color:#bb7977; font-weight:bold; '>|</span> <span style='color:#666616; '>where</span> <span style='color:#808030; '>{</span><span style='color:#007997; '>$_</span><span style='color:#44aadd; '>.</span><span style='color:#005fd2; '>Status</span> <span style='color:#44aadd; '>-eq</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>running</span><span style='color:#800000; '>"</span><span style='color:#808030; '>}</span> <span style='color:#bb7977; font-weight:bold; '>|</span> <span style='color:#005fd2; '>more</span>
 


Output Command History to text file:
Output Command History to text file:

Revision as of 13:01, 8 December 2019

Handy Powershell

Open another powershell window as admin:

Start-Process powershell -Verb runAs


Change to environment locations:

cd $Env:<vairable>

cd $Env:userprofile


Restart computer remotely:

restart-computer -Computername [hostname] -Credential [domain\username] -force


Connect to remote powershell session:

$cred=Get-Credential

$sess = New-PSSession -Credential $cred -ComputerName <remotemachinename>

Enter-PSSession $sess

<Run commands in remote session>

Exit-PSSession

Remove-PSSession $sess


Get services running on computer and display in a pauseable list:

Get-service | Where-Object {$_.Status -eq "Stopped"} | More

gsv | where {$_.Status -eq "running"} | more


Output Command History to text file:

Get-History | ForEach-Object { $_.CommandLine } > $env.USERPROFILE\testoutput.txt


Export details of users in a specific OU:

$OUpath = '<place OU path here distinguished name of ou in attribute editor>'
$ExportPath = '<place where to put output>'
Get-ADUser -Filter * -SearchBase $OUpath | Select-object DistinguishedName,Name,UserPrincipalName,sAMAccountName | Export-Csv -NoType $ExportPath

Script to ping IP address and log time and status of ping:

https://github.com/AleksPish/NetworkPingTest/blob/master/NetworkDownTest.ps1


Add Exclusions to security check from downloaded programs:

add-mppreference -exclusionpath "<full filepath - eg C:\users\downloads>"

Get members of ad group:

get-adgroupmember -identity "<name of adgroup>" | select-object name

Powershell for Admin

Get FSMO roles on which domain controllers at domain level:

Get-ADDomain | Select-Object InfrastructureMaster,PDCEmulator,RIDMaster | Format-List

Get FSMO roles on which domain controllers at forest level:

Get-ADForest | Select-Object DomainNamingMaster,SchemaMaster | Format-List

Installing packages in powershell

Can use chocolatey to get packages:

Set-ExecutionPolicy Unrestricted
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

For SSH connections:

Putty:

choco install putty

OpenSSH:

choco install openssh                # installs open ssh
refreshenv                           # reloads the environment variables
ssh remoteClient -i "MyKeyPair.pem"  # connects to remoteClient via ssh

poshSSH:

Install-Module Posh-SSH                                   # installs the posh-ssh module
Get-Command -Module Posh-SSH                              # shows all posh-ssh commandlets
New-SSHSession myclient -KeyFile "c:\data\MyKeyPair.pem"  # connect to my client with the give keyfile
Invoke-SSHCommandStream "ifconfig" -SessionId 0           # send ifconfig to the ssh session with id 0
Invoke-SSHCommand -SessionId 0 -Command "ifconfig"        # send ifconfig to the ssh session with id 0 
Invoke-SSHCommand -SessionId 0 -Command "logout"          # send logout to the ssh session with id 0
Remove-SSHSession 0                                       # removes and closes the ssh session

For firefox:

choco install firefox -y