Adventures in Powershell. These posts have been created as a record of the successful powershell commands I have used.
Thursday, December 31, 2015
Optimising AD query results.
Limiting results to queries on the active directory (AD) is often important because the results can often be relatively large sets of data and you may only need examples of the type of objects you are querying. Restricting results to the first 10 is often useful.
| Select -first 10
The querying method can also be optimised by correctly using filter and where conditions.
In a nutshell this paragraph from the post on the link below explains why.
"The –Filter and –LdapFilter parameters cause filtering to take place on the remote computer, thus reducing the amount of information that gets returned across the network to your computer. By contrast, Where-Object causes filtering to take place on your computer, which means that all the information about all your users must be returned to your machine."
http://blogs.technet.com/b/csps/archive/2010/06/06/reffilterwhere.aspx
Tuesday, December 22, 2015
Calling ICACLS.exe from powershell
Although Get-Acl and Set-Acl are available in Powershell you may still prefer to call ICACLS.EXE
This is a folder permissioning example:
$target = $env:SystemDrive + "\Apps\Folder"
$perms = "domainname\Group:(OI)(CI)(F)"
& icacls.exe ($target) /grant ($perms)
Also see this useful post http://tomandersonpro.net/ntfs-permissions-with-powershell/
and this useless page about ICACLS.EXE
https://technet.microsoft.com/en-us/library/cc753525.aspx
Note on the above
CI = container inherit which means it will apply to "This folder and subfolders"
OI = object inherit which means it will apply to "This folder, subfolders and files"
Monday, December 21, 2015
Environmental Variables
Environmental variables (EV) have powershell variables automatically assigned to them.
E.g.
The EV ALLUSERPROFILE also has a powershell variable $env:ALLUSERPROFILE and so on.
To set or update a machine environmental variable
[Environment]::SetEnvironmentVariable("TestVariableName", "My Value", "Machine")
Wednesday, December 9, 2015
Tuesday, December 8, 2015
Check your version
Certain Cmdlets may not be available in earlier versions of Powershell
To check your version of powershell, at the powershell prompt type
Get-Host
or
$PSVersionTable.PSVersion
Download Powershell 4.0https://www.microsoft.com/en-us/download/details.aspx?id=40855
Download Powershell 5.0
Latest version at the time of writing
https://www.microsoft.com/en-us/download/details.aspx?id=50395
Subscribe to:
Posts (Atom)