Monday, 11 April 2011

Testing if a variable is a specific type in Powershell

if ($variable -is [int]) {
  write-host "the variable is an integer
}


Found on http://www.computerperformance.co.uk/powershell/powershell_syntax.htm under Powershell Operators

Powershell - Creating a custom system module

First, make a new directory in [Powershellroot]\Modules with the name being the name of the module


mkdir c:\windows\system32\windowspowershell\v1.0\Modules\ADComputerObjects

Create a module manifest file

PS > new-modulemanifest -path $pshome\Modules\ADComputerObjects\ADComputerObjects.psd1


cmdlet New-ModuleManifest at command pipeline position 1 Supply values for the following parameters:
NestedModules[0]:
Author: Stuart Clarkson
CompanyName: Zen Internet
Copyright: (c) 2011 Stuart Clarkson.  All Rights Reserved
ModuleToProcess: ADComputerObjects.psm1
Description: Custom functions to find, disable and delete unused computer objects
TypesToProcess[0]:
FormatsToProcess[0]:
RequiredAssemblies[0]:
FileList[0]:

Lets see whats been created:-

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ADComputerObjects> dir





    Directory: C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ADComputerObjects




Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/04/2011     13:23       4942 ADComputerObjects.psd1


Create a .psm1 file in this directory with the functions written in



PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ADComputerObjects> ls




    Directory: C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ADComputerObjects




Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/04/2011     13:23       4942 ADComputerObjects.psd1
-a---        11/04/2011     12:56       1441 ADComputerObjects.psm1

Lets see what modules are available now:-

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ADComputerObjects> Get-Module -ListAvailable

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   ActiveDirectory           {}
Manifest   ADComputerObjects         {}
Manifest   AppLocker                 {}
Manifest   BitsTransfer              {}
Manifest   PSDiagnostics             {}
Manifest   TroubleshootingPack       {}
Manifest   WebAdministration         {}

....and import....

PS> Import-Module ADComputerObjects


MSDN site on Powershell Modules

Try these sties for more information on the manifest file