Posts tagged “module

New ‘Import-SessionCommands’ function added to SBTools module


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

The Import-SessionCommands function will import modules from other computers and export them locally if the -Keep switch us selected.

NAME
Import-SessionCommands

SYNOPSIS
Function to import commands from another computer

SYNTAX
Import-SessionCommands [-ModuleName] <String[]> [-ComputerName] <String> [[-Keep]] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
Function will import commands from remote computer from the module(s) listed.

PARAMETERS
-ModuleName <String[]>
Name(s) of the module(s) that we want to import their commands into the current
PS console.
Note that session commands will not be available in other PS instances.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-ComputerName <String>
Computer name that has the module(s) that we need to import their commands.

Required? true
Position? 2
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-Keep [<SwitchParameter>]
This is a switch. When selected, the function will export the imported module(s)
locally under “C:\Program Files\WindowsPowerShell\Modules” if it’s in the PSModulePath,
otherwise, it will export it to the default path “$home\Documents\WindowsPowerShell\Modules”
– Note 1: Exported modules and their commands can be used directly from any PS instance
after a module has been exported with the -keep switch
– Note 2: Even though a module has been exported locally, everytime you try to use one of
its commands, PS will start an implicit remoting session to the server where the
module was imported from.

Required? false
Position? 3
Default value False
Accept pipeline input? false
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

NOTES
Function by Sam Boutros
Requires PS 3.0
v1.0 – 08/17/2014
Although we need to eventually run:
Remove-PSSession -Session $Session
We cannot do that in the function as we’ll lose the imported session commands
Two things to consider:
1. The session will be automatically removed when the PS console is closed
2. If in the parent script that’s using this function a blanket Remove-PSSession
command is run, like:
Get-PSSession | Remove-PSSession
We’ll lose this session and its commands, which could cripple the parent script

————————– EXAMPLE 1 ————————–

C:\PS>Import-SessionCommands -ModuleName ActiveDirectory -ComputerName DC01

This example imports all the commands from the ActiveDirectory module from the DC01 server
So, in this PS console instance we can use AD commands like Get-ADComputer without the need
to install AD features, tools, or PS modules on this computer!

————————– EXAMPLE 2 ————————–

C:\PS>Import-SessionCommands SQLPS,Storage V-2012R2-SQL1 -Verbose

This example imports all the commands from the PSSQL and Storage modules from the MySQLServer
server into the current PS instance

————————– EXAMPLE 3 ————————–

C:\PS>Import-SessionCommands WebAdministration,BestPractices,MMAgent CM01 -keep

This example imports all the commands from the WebAdministration, BestPractices, and MMAgent
modules from the CM01 server into the current PS instance, and exports them locally.

RELATED LINKS
https://superwidgets.wordpress.com/category/powershell/

Advertisements

New ‘Get-FilesContainingText’ function added to SBTools module


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

The Get-FilesContainingText function will search one or more folders and their subfolders for a text pattern and return a list of files containing that pattern.

NAME
Get-FilesContainingText

SYNOPSIS
Function to get a list of iles containg certain text in a given set of
folders and their subfolders

SYNTAX
Get-FilesContainingText [-SearchString] <String> [[-FolderName] <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
Function returns a list of file names, each containing the search text

PARAMETERS
-SearchString <String>
The text string to search for

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-FolderName <String[]>
Name of the folder(s) to search in.
Function searches in each folder and its subfolders

Required? false
Position? 2
Default value .\
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

NOTES

Function by Sam Boutros
v1.0 – 08/16/2014

————————– EXAMPLE 1 ————————–

C:\PS>Get-FilesContainingText “import”

This example lists all the files in the current folder and
its subfolders that contain the string “import”

————————– EXAMPLE 2 ————————–

C:\PS>Get-FilesContainingText -SearchString “cheese” -FolderName “d:\Sandbox”,”\\MyServer1\install” -Verbose

This example lists all the files in the folders”d:\Sandbox” and
“\\MyServer1\install” and their subfolders that contain the string “cheese”

RELATED LINKS
https://superwidgets.wordpress.com/category/powershell/


New ‘Get-SBIPInfo’ function added to SBTools module retrieves computer IPv4 and IPv6 information


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

The Get-SBIPInfo function will retrieve computer IP information for one or more computers.

Sample output:

get-sbipinfo1

NAME
Get-SBIPInfo

SYNOPSIS
Function to get computer IP information.

SYNTAX
Get-SBIPInfo [[-ComputerName] <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
Function to get computer IP information.
Function returns an object that has the following properties:
ComputerName, AdapterDescription, IPAddress, IPVersion, SubnetMask, CIDR, MAC, DefaultGateway, DNSServer

PARAMETERS
-ComputerName <String[]>
Name(s) of the computer(s) to be used to get their IP information.

Required? false
Position? 1
Default value $env:COMPUTERNAME
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS
System.String[]

OUTPUTS
System.Object

NOTES
Function by Sam Boutros
V 1.0
08/12/2014

————————– EXAMPLE 1 ————————–

C:\PS>Get-SBIPInfo

This example retuns the current computer IP information

————————– EXAMPLE 2 ————————–

C:\PS>Get-SBIPInfo xhost11 | Out-GridView

This example displays IP information of computer xHost11

————————– EXAMPLE 3 ————————–

C:\PS>Get-Content .\computers.txt | Get-SBIPInfo

This example returns IP information of every computer listed in the computers.txt file

————————– EXAMPLE 4 ————————–

C:\PS>Get-SBIPInfo | Where-Object { $_.IPversion -eq 4 } | Select-Object { $_.IPAddress }

This example lists the IPv4 address(es) of the local computer


New ‘ Test-SBVHDIntegrity’ function added to SBTools module tests VM VHD(x) files’ integrity


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

The Test-SBVHDIntegrity function will test the drive files of a Hyper-V virtual machine and verify the specified file exists and that there are no problems with it.

This is a function by Jeff Hicks originally published here, modified by Sam Boutros 08/09/2014, added functionality to work on VMs from other than localhost.

NAME
Test-SBVHDIntegrity

SYNOPSIS
Test VHD files for integrity

SYNTAX
Test-SBVHDIntegrity [-VM] <Object[]> [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
Test the drive files for a Hyper-V virtual machine and verify the specified
file exists and that there are no problems with it.

PARAMETERS
-VM <Object[]>
VM object. Function will check integrity of input VM disk files.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

————————– EXAMPLE 1 ————————–

C:\PS>Get-VM | Test-SBVHDIntegrity | Out-Gridview

————————– EXAMPLE 2 ————————–

C:\PS>Get-VM -ComputerName xHost11,xHost12 | Test-SBVHDIntegrity | Out-Gridview

This example displays disk integrity results for each VM on the Hyper-V hosts xHost11 and xHost12

————————– EXAMPLE 3 ————————–

C:\PS>Get-VM -ComputerName (Get-Content .\computers.txt) | Test-SBVHDIntegrity | Out-Gridview

This example displays disk integrity results for each VM on the Hyper-V hosts listed n the .\computers.txt file

————————– EXAMPLE 4 ————————–

C:\PS>Get-VM | Test-SBVHDIntegrity | where {(-NOT $_.TestPath) -OR (-NOT $_.TestVHD)}

This examples displays disks that fail either the TestPath or TestVHD checks


New ‘Get-SBRDPSession’ function added to SBTools module to get RDP sessions on one or more computers


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

Get-SBRDPSession:

This is a function to get RDP sessions on one or more computers. It returns object collection, each corresponding to a session. Object properties: ComputerName, UserName, SessionName, ID, and State. ID refers to RDP session ID. State refers to RDP session State

Parameters:

This function accepts the following 2 parameters:

  1. Parameter ComputerName
    If absent, function assumes localhost.
  2. Parameter State
    Filters result by one or more States (Disconnected, Connected, Active, Listen)
    Valid options are:
    Disc
    Conn
    Active
    Listen

Examples:

  • Get-SBRDPSession -ComputerName xHost15 -State Disc | FT

This example lists disconnected RDP sessions on the computer xHost16 in table format.

Sample output:

Get-SBRDPSession4

  • Get-SBRDPSession -state Active,Disc | FT

This example lists RDP sessions on the local machine, and returns those with State Active or Disc in table format.

Sample output:

Get-SBRDPSession1

  • Get-SBRDPSession xhost11,xhost12 | FT

This example lists RDP sesions on the computers xHost11 and xHost12 and outputs the result in table format.

Sample output:

Get-SBRDPSession2

  • Get-SBRDPSession (Get-Content .\computers.txt) Disc -Verbose | FT

This example reads a computer list from the file .\computers.txt and displays disconnected sessions
Get-SBRDPSession3


New ‘Test-SBDisk’ function added to SBTools module tests disk IO performance


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

Test-SBDisk:

This is a function to test disk IO performance. It uses other functions in the SBTools module like Log and New-SBSeed.

This function tests disk IO performance by creating random files on the target disk and measuring IO performance. It leaves 2 files in the WorkFolder:

  • A log file that lists script progress, and
  • a CSV file that has a record for each testing cycle

Parameters:

This function accepts 5 parameters:

  1. Parameter WorkFolder
    This is where the test and log files will be created.
    Must be on a local drive. UNC paths are not supported.
    The function will create the folder if it does not exist
    The function will fail if it’s run in a security context that cannot create files and folders in the WorkFolder
    Example: c:\support
  2. Parameter MaxSpaceToUseOnDisk
    Maximum Space To Use On Disk (in Bytes)
    Example: 10GB, or 115MB or 1234567890
  3. .Parameter Threads
    This is the maximum number of concurrent copy processes the script will spawn.
    Maximum is 16. Default is 1.
  4. Parameter Cycles
    The function generates random files in a subfolder under the WorkFolder.
    When the total WorkSubFolder size reaches 90% of MaxSpaceToUseOnDisk, the script deletes all test files and starts over.
    This is a cycle. Each cycle stats are recorded in the CVS and log files
    Default value is 3.
  5. Parameter SmallestFile
    Order of magnitude of the smallest file.
    The function uses the following 9 orders of magnitude: (10KB,100KB,1MB,10MB,100MB,1GB,10GB,100GB,1TB) referred to as 0..8
    For example, SmallestFile value of 4 tells the script to use smallest file size of 100MB
    The script uses a variable: LargestFile, it’s selected to be one order of magnitude below MaxSpaceToUseOnDisk
    To see higher IOPS select a high SmallestFile value
    Default value is 4 (100MB). If the SmallestFile is too high, the script adjusts it to be one order of magnitude below LargestFile

Examples

  • Test-SBDisk “i:\support” 3GB
    This example tests the i: drive, generates files under i:\support, uses a maximum of 3GB disk space on i: drive.
    It runs a single thread, runs for 3 cycles, uses largest file = 100MB (1 order of magnitude below 3GB entered), smallest file = 10MB (1 order of magnitude below largest file)
    Sample screen output:
    Test-SBDisk1Sample log file content:
    2014.08.06 09:20:30 AM: Computer: XHOST16, WorkFolder = i:\support, MaxSpaceToUseOnDisk = 3GB, Threads = 1, Cycles = 3, SmallestFile = 10MB, LargestFile = 100MB
    2014.08.06 09:20:46 AM: Cycle #1 stats:
    2014.08.06 09:20:46 AM: Duration 10.02 seconds
    2014.08.06 09:20:46 AM: Files copied 2.70 GB
    2014.08.06 09:20:46 AM: Number of files 25
    2014.08.06 09:20:46 AM: Average file size 110.70 MB
    2014.08.06 09:20:46 AM: Throughput 276.33 MB/s
    2014.08.06 09:20:46 AM: IOPS 8.84k (32KB block size)
    2014.08.06 09:20:59 AM: Cycle #2 stats:
    2014.08.06 09:20:59 AM: Duration 9.83 seconds
    2014.08.06 09:20:59 AM: Files copied 2.70 GB
    2014.08.06 09:20:59 AM: Number of files 25
    2014.08.06 09:20:59 AM: Average file size 110.70 MB
    2014.08.06 09:20:59 AM: Throughput 281.60 MB/s
    2014.08.06 09:20:59 AM: IOPS 9.01k (32KB block size)
    2014.08.06 09:21:15 AM: Cycle #3 stats:
    2014.08.06 09:21:15 AM: Duration 12.12 seconds
    2014.08.06 09:21:15 AM: Files copied 2.70 GB
    2014.08.06 09:21:15 AM: Number of files 33
    2014.08.06 09:21:15 AM: Average file size 83.87 MB
    2014.08.06 09:21:15 AM: Throughput 228.25 MB/s
    2014.08.06 09:21:15 AM: IOPS 7.30k (32KB block size)
    2014.08.06 09:21:15 AM: Testing completed successfully.
    Sample CSV file content:
    Cycle # Duration (sec) Files (GB) # of Files Avg. File (MB) Throughput (MB/s) IOPS (K) (32KB blocks) Machine Name Start Time End Time
    1 10.02 2.7 25 110.7 276.33 8.84 XHOST16 8/6/2014 9:20 8/6/2014 9:20
    2 9.83 2.7 25 110.7 281.6 9.01 XHOST16 8/6/2014 9:20 8/6/2014 9:20
    3 12.12 2.7 33 83.87 228.25 7.3 XHOST16 8/6/2014 9:20 8/6/2014 9:21
  • Test-SBDisk “i:\support” 11GB -Threads 8 -Cycles 5 -SmallestFile 4
    This example tests the i: drive, generates files under i:\support, uses a maximum of 11GB disk space on i: drive, uses a maximum of 8 threads, runs for 5 cycles, and uses SamllestFile 100MB.

New ‘New-SBSeed’ function added to SBTools module creates random files for disk IO performance


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

New-SBSeed:

This is a recursive function to create files for disk performance testing. Files will have random numbers as content. File name will have ‘Seed’ prefix and .txt extension.

Function will start with the smallest seed file of 10KB, and end with the Seed file specified in the -SeedSize parameter.

Function will create seed files in order of magnitude starting with 10KB and ending with ‘SeedSize’.

Files will be created in the current folder.

This function takes 1 parameter: SeedSize. This is the size of the largest seed file generated. Accepted values are:

10KB
100KB
1MB
10MB
100MB
1GB
10GB
100GB
1TB

Example

New-SBSeed -SeedSize 10MB -Verbose

This example creates seed files starting from the smallest seed 10KB to the seed size specified in the -SeedSize parameter 10MB.
To see the output you can type in:

Get-ChildItem -Path .\ -Filter *Seed*

Sample output:

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 8/6/2014 8:26 AM 102544 Seed100KB.txt
-a— 8/6/2014 8:26 AM 10254 Seed10KB.txt
-a— 8/6/2014 8:39 AM 10254444 Seed10MB.txt
-a— 8/6/2014 8:26 AM 1025444 Seed1MB.txt


New ‘Log’ function added to SBTools module logs input string to file and display it to screen in different colors


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

Log:

This is a function to log input string to file and display it to screen. Log entries in the log file are time stamped. Function allows for displaying text to screen in different colors.

The function accepts 3 parameters:

  1. String: Required. The string to be displayed to the screen and saved to the log file
  2. Color: The color in which to display the input string on the screen
    Default is White
    Valid options are
    Black
    Blue
    Cyan
    DarkBlue
    DarkCyan
    DarkGray
    DarkGreen
    DarkMagenta
    DarkRed
    DarkYellow
    Gray
    Green
    Magenta
    Red
    White
    Yellow
  3. LogFile
    Path to the file where the input string should be saved.
    Example: c:\log.txt
    If absent, the input string will be displayed to the screen only and not saved to log file

Examples

  • Log -String “Hello World” -Color Yellow -LogFile c:\log.txt
    This example displays the “Hello World” string to the console in yellow, and adds it as a new line to the file c:\log.txt
    If c:\log.txt does not exist it will be created.
    Log entries in the log file are time stamped. Sample output:
    2014.08.06 06:52:17 AM: Hello World
  • Example
    Log “$((Get-Location).Path)” Cyan
    This example displays current path in Cyan, and does not log the displayed text to log file.
  • Example
    “Java process ID is $((Get-Process -Name java).id )” | log -color Yellow
    Sample output of this example:
    Java process ID is 4492
  • Example (This example uses Get-SBVHD function)
    “Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘$((Get-SBVHD CM01 d).VHDPath)'” | log -color Green -LogFile D:\Sandbox\Serverlog.txt
    Sample output of this example:
    Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘D:\VMs\Virtual Hard Disks\CM01_D1.VHDX’
    and the same is logged to file D:\Sandbox\Serverlog.txt as in:
    2014.08.06 07:28:59 AM: Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘D:\VMs\Virtual Hard Disks\CM01_D1.VHDX’