Let’s start with Powershell – part 3
Reading from user, pipes, enviroment variables.
Short Summary
You have found out about basics of the powershell, what it is, how to run it and how commandlets are build. Also, you allready know about types of variables. Now let’s deal with Reading from user, pipes and envirment variables.
Reading user input
Reading data from user is essensial, when we wan’t to create script, that interacts with the user. For that, we use read-host command
The read-host command echoes back what was typed, however if we assign that value to a variable, say “$name” then we can capture the user input for later usage.
PS D:\> Read-host "Provide Your name" Provide Your name: Bartosz Bartosz PS D:\> $name=Read-host "Provide Your name" Provide Your name: Bartosz PS D:\> $name Bartosz PS D:\> Write-host "Hello $name" Hello Bartosz PS D:\>
The PowerShell environment
Within a shell, the “environment” is the term to describe the current settings.
These settings are exposed in environment variables. So for example the variable username contains the username of the current logged on user.
To list all available environment variables we use get-item env:
PS H:\> Get-Item env: Name Value ---- ----- SystemDrive C: ProgramFiles(x86) C:\Program Files (x86) USERDNSDOMAIN test ProgramW6432 C:\Program Files PROCESSOR_IDENTIFIER Intel64 Family 6 Model 60 Stepping 3, GenuineIntel APPDATA C:\Users\PawlakB\AppData\Roaming TMP C:\Users\PawlakB\AppData\Local\Temp PROCESSOR_ARCHITECTURE AMD64 PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL HOMEPATH \ ADAPTIVACLIENT "C:\Program Files (x86)\Adaptiva\AdaptivaClient" CommonProgramFiles C:\Program Files\Common Files TEMP C:\Users\PawlakB\AppData\Local\Temp USERPROFILE C:\Users\PawlakB LOGONSERVER \\testdc USERNAME pawlakb SystemRoot C:\Windows FP_NO_HOST_CHECK NO CommonProgramFiles(x86) C:\Program Files (x86)\Common Files UATDATA C:\Windows\CCM\UATData\D9F8C395-CAB8-491d-B8AC-179A1FE1BE77 ProgramData C:\ProgramData windows_tracing_logfile C:\BVTBin\Tests\installpackage\csilogfile.log HOMESHARE \\testdc\users\PawlakB COMPUTERNAME TEST ALLUSERSPROFILE C:\ProgramData CommonProgramW6432 C:\Program Files\Common Files windows_tracing_flags 3 SESSIONNAME Console PROCESSOR_REVISION 3c03 HOMEDRIVE H: windir C:\Windows NUMBER_OF_PROCESSORS 4 OS Windows_NT COMMONDIR C:\ ProgramFiles C:\Program Files ComSpec C:\Windows\system32\cmd.exe PSModulePath C:\Users\PawlakB\Documents\WindowsPowerShell\Modules;C:\Program Files\Wind... deployment.expiration.check... false USERDOMAIN TEST PROCESSOR_LEVEL 6 Path C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Wind... LOCALAPPDATA C:\Users\PawlakB\AppData\Local USERDOMAIN_ROAMINGPROFILE TEST PUBLIC C:\Users\Public
To get only one variable we can use Get-item env:\username
PS H:\> Get-item env:\username Name Value ---- ----- USERNAME pawlakb
Or in some simpler way $env:username
PS D:\tools> $env:USERNAME pawlakb PS D:\tools> $env:USERDOMAIN TEST PS D:\tools> $env:OS Windows_NT
Get-Item returns a Hashtable, when $env:name returns a string value:
PS D:\> (get-item env:\username).gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DictionaryEntry System.ValueType PS D:\> $env:username.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object
Pipes
Pipe “|” is the way to pass output from one command to another, and another… and so on…
PS H:\> $a= dir PS H:\> $a Directory: H:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2017-04-10 12:58 WINDOWS -a--- 2018-04-18 11:02 674 a.txt ----- 2016-01-25 13:30 2175 logo1.jpg ----- 2016-01-08 22:35 278698 Upgrade-20160108-131637-472-error.log PS H:\> $a| select name Name ---- WINDOWS a.txt logo1.jpg Upgrade-20160108-131637-472-error.log PS H:\> $a| Select name|out-file a.txt PS H:\> type a.txt Name ---- WINDOWS a.txt logo1.jpg Upgrade-20160108-131637-472-error.log
In this example I read the dir output to the variable $a, then I have selected only one property (Name). At the last par I have send it to a file a.txt
TIP
While using pipes, you wil be able to move between objects if they are connected( fe. choose one Exchange database|move to users in that database|select properties based on specific criteria|save CSV file.