The origins of PowerShell lie in the Monad project which you can learn about here: https://www.jsnover.com/Docs/MonadManifesto.pdf
PowerShell Base CmdLets and associated Pipeline parsing CmdLets provide powerful Windows administration tools
These can be used on-the-fly to glean and share information about a problem or the state of your machine(s) and network or they can be crafted into useful scripts that run on a schedule to report on the status of applications and services, run backup and ETL tasks as well as myriad other (often critical) scheduled jobs that happen routinely behind the scenes to keep IT operations organized and running.
You may for instance, want to have a script run every few hours that gathers statistics about throughput and storage and then alert admin users if a certain threshold is exceeded. Or, in Azure, you may want to utilize a scripted template (like Azure ARM and associated CLI commands) to configure new Azure resources and their environments.
Useful cmdlets:
#check security levelGet-ExecutionPolicy
#elevate security access levelSet-ExecutionPolicy Unrestricted
#get information on any serviceGet-Service -Name PhoneSvc
#get the same log info seen in eventvwrGet-EventLog -Log "Application"
#get process informationGet-Process -ComputerName MYCOMPUTER
#stop process like cmd.exe killStop-Process -Name “notepad”
#get drive information of the drives connected to the current PS sessionGet-PSDrive
#get information on any powershell cmdlt, function or moduleGet-Help -Name Streaming
#get all the installed commandsGet-Command
#connect to your azure account with the "az" azure cmdlt
Connect-AzureRmAccount
#upload blob content to storage
Set-AzStorageBlobContent -File "D:\_TestImages\Image002.png" `
-Container $containerName `
-Blob "Image002.png" `
-Context $ctx
-StandardBlobTier Cool
#download blob content from storage
Get-AzStorageBlobContent -Blob "Image002.png" `
-Container $containerName `
-Destination "D:\_TestImages\Downloads\" `
-Context $ctx
#stop a sql server instanceStop-SqlInstance -ServerInstance MSSQL01
#clear screenClear-Host
#pingTest-NetConnection
#telnetTest-NetConnection -Port
#tracertTest-NetConnection -TraceRoute
#ipconfigGet-NetIPAddress
#nslookupResolve-DnsName -Name "Hostname"
#netstatGet-NetTCPConnection
#flushdnsClear-DnsClientCache
#ip release/renewInvoke-Command -ComputerName -ScriptBlock {ipconfig /release}
Invoke-Command -ComputerName -ScriptBlock {ipconfig /renew}
#disable/enable network cardInvoke-Command -ComputerName -ScriptBlock {ipconfig /release}
Invoke-Command -ComputerName -ScriptBlock {ipconfig /renew}
Additionally, it is often useful to implement piping of command output, especially in CI/CD toolchain scripting where scripts feed their output (which become the next script's argument(s)) to the next script in the chain.
For example:
#export a list to .csv fileGet-Service | Export-CSV c:\20200912_ServiceSnapshot.csv
#be more selective with Select-Object module and pipe that to the csvGet-Service | Select-Object Name, Status | Export-CSV c:\20200912_ServiceStatusSnapshot.csv
#get event information and pipe method (of each log event) info to the consoleGet-EventLog -log system | gm -MemberType Methods
#get a process and stop itGet-Process notepad | Stop-Process
#delete all files matching some Regex patternGet-ChildItem $Path | Where{$_.Name -Match "someFileName.txt"} | Remove-Item
References:
No comments:
Post a Comment