Archive for July, 2014

Powershell script to change printer IP address


netprintA question was posted in Powershell.org Q&A forum:

I have a weird question. Can you modify object properties. the command
Get-PrinterPort 192.* | fl *
prints out all the attributes of the printer port. but I can’t modify them. I don’t want to create a new port and start from scratch, just modify the existing object. Or maybe export the export and then import it, modified, into the new one.

This script does just that. It can be downloaded from the Microsoft Script Center Repository.

The reason we cannot modify the existing printer port object is that the Get-Printer commandlet uses the MSFT_TCPIpPrinterPort class. This class has the 2 properties we want to change (HostAddress and Name) but they’re read-only.

Even if we try to use the Win32_TCPIpPrinterPort class, as in:

$OldIP = “192.168.0.95”
$NewIP = “192.168.0.99”
$objPrinterPort = Get-WmiObject -Class Win32_TCPIpPrinterPort | Where-Object {$_.Name -eq “IP_$OldIP”}
$objPrinterPort.psbase.scope.options.EnablePrivileges = $true
$objPrinterPort.HostAddress = $NewIP
$objPrinterPort.Name = “IP_$NewIP”
$objPrinterPort.Put()

This does not modify the existing Printer Port, it creates a new one..

Example script usage:

.\Change-PrinterIP.ps1 192.168.0.95 192.168.0.99

Script output looks like:

Change-PrinterIP

 


Test-Disk powershell script to test disk IO performance


disk-io-performanceThis script tests disk IO performance by creating random files on the target disk and measuring IO performance. The script can be downloaded from the Microsoft Script Center Repository.  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.

Warning:

This script will delete all subfolders in the work-folder. Make sure to run it in a new folder.

For help type in;

help .\Test-Disk.ps1 -full

Example   .\Test-Disk.ps1 “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)

Example   .\Test-Disk.ps1 “i:\support” 11GB 8 5 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.

Script output looks like:

Test-Disk



Powershell script to generate HTML report on files in given folders with specific extensions and size


11/03/2014 – v1.2

Added ‘Report run time’ at the bottom
Fixed bug when no files are found that match criteria
Turned into function
Bundled with EnhancvedHTML2
Added SizeUnit parameter

Screen output will look like:

ROF1

The HTML report will look like:

ROF2


A question was posted a few days ago on the Powershell.org Q&A forum:

I’m trying to get people to clean up old files. Never an easy job. To help them out, I want to generate reports of their department folders. One report would list their Powerpoint files, another PDFs and a third, zip files.

Many of us have been in this situation where data sprawl goes a bit out of control. This script helps produce HTML reports that can be customized to report on:

  • Files in several folders,
  • With specific file extensions,
  • Greater than a specific file size,
  • File properties reported on can also be customized

The script can be downloaded from the Microsoft Script Center Repository.

Sam Boutros (left) and Don Jones (right) - TechEd 2014 - Houston

Sam Boutros (left) and Don Jones (right) – TechEd 2014 – Houston

The script uses EnahncedHTML2 module which is written by Don Jones in 9/2013. I first met Don at TechEd 2014 in Houston where he delivered several sessions that I attended and enjoyed.  In spite of being one of the leading experts on Microsoft business technologies, he’s a down to earth guy who would take the time to answer a question or help out when needed.

To install EnhancedHTML2 module:

  1. Download it from the Powershell.org public OneDrive. Look under the Creating HTML Reports folder.
  2. Check your Powershell Module Path. Run $env:PSModulePath.Split(“;”) for example:ModPath
  3. Create a folder named “EnhancedHTML2” under any of the listed paths like c:\Program Files\WindowsPowerShell\Modules\EnhancedHTML2
  4. Copy the 2 files EnhancedHTML2.psd1 and EnhancedHTML2.psm1 to this folder (part of the downloaded files from #1 above)
  5. Finally run Import-Module EnahncedHTML2


Get-Busy.ps1 powershell script to create files on many PCs and collect metrics


This script uses Busy.ps1 which is a script that I posted earlier. This script can be downloaded from the Microsoft TechNet Gallery. To use this script you need to edit the 4 data entry lines on top:

GetBusy

  • $WorkFolder = “e:\support” # Folder on each VM where test files will be created
  • $MaxSpaceToUseOnDisk = 20GB # Maximum amount of disk space to be used on each VM’s $WorkFolder during testing
  • $VMPrefix = “V-2012R2-LAB” # We’ll use that to scope this script (only running VMs with name matching this Prefix will be processed)
  • $LocalAdmin = “administrator” # This is the local admin account on the VMs. You can also use a domain account here if you like.

The script requires 1 positional parameter to indicate whether you wish to start the testing on the VMs or stop it. For example to start:

.\get-busy.ps1 start

GS-017e40

 To end the testing, use:

 .\get-busy.ps1 stop

The script will reach out to the VMs being tested and stop the busy.ps1 script, collect the test results, and cleanup the $Workfolder on each VM.

The script generates 2 log files:

  • A general log file that contains the messages displayed on the screen about each action attempted.
  • A CSV file that contains the compiled test results from all the CSV files generated by each busy.ps1 script on each of the tested VMs

Here’s an example of the compiled CSV file Get-busy_20140714_071428PM.csv


Powershell script to generate file read/write activity to test storage performance


This script generates file read/write activity to test storage performance. Set the $WorkFolder and $MaxSpaceToUseOnDisk variables on top to indicate the folder where test files will be created and maximum amount of disk space to be used on $WorkFolder during testing respectively.

The script can be run with the -verbose switch to show more information during processing, like:

.\busy2.ps1 -verbose

As the $WorkFolder exceed 90% of the capacity indicated by the $MaxSpaceToUseOnDisk variable, the scripts deletes all test files and starts a new cycle. the script can be stopped by clicking CTRL-C or setting the registry key HKLM:\Software\Microsoft\Busy to 0

The script includes a recursive function to generate seed files of (decimal) exponential sizes to be used for file copy activities.

The script generates 2 log files. The first one has details on the files created. The second one is a CSV file that has a record for every cycle the script goes through, including cycle duration, sum of generated file sizes, number of files generated on this cycle, average file size, disk throughput, IOPS,…

Here’s an example CSV file output:

Cycle # Duration (sec) Files (GB) # of Files Avg. File (MB) Throughput (MB/s) IOPS (K) Machine Name Start Time End Time
1 337.67 95.35 19 5138.85 289.15 4.63 HV-LAB-05 07/10/2014 19:54 07/10/2014 20:00

The script can be downloaded from the Microsoft TechNet Gallery.

Busy2


7/15/2014:

Version 1.2 is here

The script displays output to the screen like:

It also logs the same to a text file like Busy_HV-LAB-05.txt and enters the statistics in a CSV file like Busy_HV-LAB-05.csv


Powershell script to live migrate all running VMs including storage from one host to another


Powershell Script to live migrate all running VMs; including storage; from one host to another. Enter the source and destination host names (or IPs) in lines 4 and 5. Enter the drive letters of the destination host where you wish to live migrate the source host VMs to in line 6.

Moveall2

You can download this script from the Microsoft TechNet Gallery.

The script live migrates the VMs:

Moveall1

It also leaves 2 files on the folder where it runs:

– A log file that shows different information about the source and destination hosts, and the VMs being migrated.

– A CSV file that shows one record per migrated VM listing the source path of each of its disks. This may be useful for failback..


Powershell script to setup NFS share on Server 2012 R2


The following script sets up NFS share on Server 2012 R2:

NFS1

# Script to setup NFS share on Server 2012 R2
# Sam Boutros – 7/7/2014
# References: http://technet.microsoft.com/en-us/library/jj603081.aspx,
# http://technet.microsoft.com/en-us/library/jj574143.aspx#BKMK_deploy
#
$NFSFolder = “d:\shares\NFS1” # Tye in the path to the folder to be shared via NFS
$NFSShare = “nfs1” # Type in the share name you wish to use
# End Data entry section
#
Import-Module ServerManager
Add-WindowsFeature FS-NFS-Service
Import-Module NFS
if (!(Test-Path $NFSFolder)) {New-Item -ItemType directory -Path $NFSFolder}
New-NfsShare -name $NFSShare -Path $NFSFolder
$ShareName = $env:COMPUTERNAME + “:/” + $NFSShare
Write-Output “NFS share has been created: $ShareName”

You can see the new share in Server Manager under File and Storage Services:

NFS7

The default NFS share properties are:

NFS2

NFS3

Under Share Permissions, I clicked Add, entered the IP address (or name) of the Linux host that needs to connect to this 2012 R2 NFS share, and selected the desired access (read/write):

NFS4

No changes to NTFS permissions:

NFS5

Finally, I enabled the following firewall rule to allow me to ping this server:NFS8

Next, I setup a Red Hat 6.5 VM to test with.  I picked v6.5 because

  1. It comes with the Hyper-V integration built in – no need for manual RTM installation,
  2. The Hyper-V integration server for 6.5 kernel allows for Host-based VSS backup – no need for guest agent inside the Linux machine. We get crash consistent backup (not application consistent backup), but that’s a big improvement from Hyper-V integration service for kernel version 6.4 and older,
  3. The Hyper-V integration service supports dynamic RAM. Again, with kernel versions 6.4 and older, we can only use static RAM with Linux guests

I downloaded the media ISO Boot and DVD files from Red Hat,

created VM with 2 DVDs so that I can mount both media during setup, followed the simple installation steps, rebooted

Next, I used the console (and later on Putty to SSH) to the new Red Hat machine, logged in as root,

NFS9

I ran the following commands:

ifconfig -a => show interfaces
ifconfig eth0 10.5.19.36 netmask 255.255.255.0 => to setup IP address and mask
ping 10.5.19.38 => confirm connection to NFS server (enabled ping rule in Windows firewall)
mount -t ext4 => to show partitions formatted with ext4 file system
mkdir /pub => to create local mount point directory /pub
mount 10.5.19.38:/nfs1 /pub (must have added 10.5.19.36/RW permission on NFS share on Server 2012)
cd /pub => to change directory to /pub
ls => to see files on remote NFS share
echo hello >test1.txt => to test creating a text file named test1

You might want to edit /etc/fstab and modify the following line to match your configuration. For example:

10.5.19.38:/nfs1 /pub nfs defaults 0 0

The files were visible also on the 2012 R2 server.

 


Powershell script to rename many computers and join them to AD domain


This script will go through all VMs on the current Hyper-V host, select those that match a given name pattern, rename them to match the VM Name (in Hyper-V), and join them to the domain.

CVM3

The script can be downloaded from the Microsoft TechNet Gallery.

The script leaves a log file in the folder where it runs that shows details of actions performed and their success/failure status.

In the V1.1 update, I added code to initialize and format VM disks..

The VMs can be created in bulk as shown in this post.

This script needs a running VM acting as DC and DHCP server as shown in this post.