26 January 2015

Active Directory schema attribute details

$schema =[DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetCurrentSchema()

$schema.FindClass('user').optionalproperties | Where-Object {$_.name -eq 'employeeid'}

11 January 2015

Scheduled jobs in powershell v3

PSScheduledJob is the module name.

Scheduled jobs can be viewed in Task Scheduler GUI.

Scheduled jobs cmdlet can only be used for the path: Microsoft\Windows\PowerShell\ScheduledJobs

SheduledTask module can be used to manage all other scheduled tasks (available only in Windows 8 and Server 2012).

Job definition and output is stored on disk in different files that can be found on:

%UserProfile%\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\JobName

09 January 2015

About modules in powershell v3

Powershell have two default location in which it search for modules: one for the system and one for the user currently logged on:

  • system: %windir%\System32\WindowsPowerShell\v1.0\Modules
  • user: %UserProfile%\Documents\WindowsPowerShell\Modules
these values are stored in a environment variable $ENV:PSModulePath

Modules from this location will be automatically imported in powershell v3.

Get-Command cmdlet will return commands from imported or modules available in the two paths.

In powershell v3, modules can be imported (or listed) from another computer trough a cimsession or powershell session:

Import-Module -CimSession $session -Name ModuleName

Get-Module -CimSession $session -ListAvailable

PowerShell Web Access

PSWA (PowerShell Web Access) emulates a powershell console in a web browser.

PSWA is a feature (not a role) available only on Windows Server 2012.
It requires web server role (IIS).

Setup steps:

  • install PSWA windows feature
  • install the PSWA web application 
  • add PSWA authorization rules to control and secure access.

06 January 2015

Out-GridView enhancement in powershell v3

In powershell v3 cmdlet Out-GridView had a great enhancement with OutputMode parameter.

When used, this parameter instructs the cmdlet to send the items from the interactive window down the pipeline as input to another command.

By default, this cmdlet does not generate any output bat by using this OutputMode parameter, the user can select one or more items that will be send down the pipeline.

OutputMode can be set to:

  • None - no items.
  • Single - zero or one item can be selected.
  • Multiple - zero, one or more items can be selected to be send down the pipeline to the next cmdlet.
Example (will try to stop selected processes):

Get-Process | Out-GridView -OutputMode Multiple | Stop-Process

CIM cmdlets in powershell v3

In powershell v3 WMI cmdlets are replaced by CIM cmdlet which are used a little bit different then WMI cmdlets.

On example is the method invocation which cannot be done by referencing the object that contain the WMI data:

$wmi_object.method_name

CIM objects don't have all the relevant and useful methods that WMI objects have.

To discover methods that can be used for a specific class name we use:

Get-CimClass -ClassName Win32_Process | Select-Object -ExpandProperty CimClassMethods

after discovering the methods available we can invoke a method by using the Invoke-CimMethod cmdlet:

$CIMProcess | Invoke-CimMethod -MethodName Terminate

04 January 2015

Powershell v3 features



  • Ordered hash tables can be created by casting with [ordered] keyword:

    $hash = [ordered]@{firstname = 'John' ; lastname = 'Smith'}
  • Custom objects can be created more easy as hash table by casting with [PSCustomObject]
    it will be automatically ordered.

    $a = [PSCustomObject]@{firstname = 'John' ; lastname = 'Smith'}
  •  default parameters values can be set for cmdles.

    $PSDefaultParameterValues['Get-ADUser:Properties']='EmployeeID'

    will only be available in current session but can be added to profile and be loaded at start up.
    one disadvantage is that if you use that parameter in your comdlets the default value that was setup will be overwritten.
      
  • Count and Length property is available for all objects (even if the object is not an array).
    All objects can be indexed into with [ ]
    If the object already have an Count or Length property, they will remain unchanged,
  • ISE have now tabs - can be used for different remote sessions.

    tabs can be renamed: $psise.PowerShellTabs[0].DisplayName = 'Production Scripts'
    new tab can be opened: $PSISE.PowerShellTabs.Add()
  • in ISE explicit code regions can be used to allow collapsing between any two points

    #region some region description can be inserted here
    #endregion
  • ISE automatically highlight matching braces ( ) { } [ ] when cursor is in front of them.
  • current object in pipeline that use to be referred with $_ can also be referred with $PSItem variable.

02 January 2015

Update poweshell help for computers not connected to internet


Starting with powershell v3 the help files must be downloaded as separate files an can also be updated in case something has changed.

if the computer have a internet connection available Update-Help cmdlet will download and install the help or just update it if is allready installed.

if the computer does not have an internet connection available then the help files can be downloaded on a computer that have an internet connection available and then transferred to an internal network share or directly to the computer that need to update the help files.

to save the help files from the internet use:

Save-Help -DestinationPath c:\temp\PowershellHelpFiles

destination path can also be a network location \\fileserver\FolderName

After that, on the computer that need to be updated run a powershell console with elevated rights (RunAs Administrator) and type:

Update-Help -SourcePath c:\temp\PowershellHelpFiles

source path can also be a network location \\fileserver\FolderName