Powershell: Difference between revisions

From Piszczynski
piszczynski>Aleks
piszczynski>Aleks
No edit summary
Line 1: Line 1:


== Handy Powershell ==
== Handy Powershell ==#
 
Change to environment locations:
<pre>
cd $Env:<vairable>
cd $Env:userprofile
</pre>
 
Restart computer remotely
Restart computer remotely
<pre>
<pre>
Line 44: Line 51:
<pre>
<pre>
get-adgroupmember -identity "<name of adgroup>" | select-object name
get-adgroupmember -identity "<name of adgroup>" | select-object name
</pre>
== Installing packages in powershell ==
Can use chocolatey to get packages:
<pre>
Set-ExecutionPolicy Unrestricted
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
</pre>
For SSH connections:
<pre>
choco install openssh                # installs open ssh
refreshenv                          # reloads the environment variables
ssh remoteClient -i "MyKeyPair.pem"  # connects to remoteClient via ssh
</pre>
poshSSH:
<pre>
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
</pre>
</pre>

Revision as of 23:38, 3 December 2019

== Handy Powershell ==#

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


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

Installing packages in powershell

Can use chocolatey to get packages:

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

For SSH connections:

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