Powershell – Executing a batch file on remote machine

Do you have a batch file which you want to run on multiple remote machines? The below powershell script can simplify this task. I have used this script to remotely repair SCCM client on multiple machines by executing batch files.

The steps are quite simple

  • Copy the below code to powershell script file.
  • Provide list of machines in computers.txt file in same folder where you kept the script.
  • Copy your batch file ‘MyFile.bat’  in same folder.
  • Run the script.

Code:

#Get current path
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$Inputfile = $directorypath + “\computers.txt”
$BatchFile = “c:\windows\temp\MyFile.bat”
$source = $directorypath + “\Myfile.bat”
Get-Content $inputfile | ForEach-Object {
$computer = $_
$TargetPath = “\\$computer\admin$\temp\”
Write-Host  “===============================”
#Copy source
if (Test-Path -Path $targetPath)
{
Copy-Item -Path $source $targetPath -force
#Run batch file
Write-Host “$computer : Executing batch file $batchfile”
([WMICLASS]”\\$computer\ROOT\CIMV2:win32_process”).Create(“cmd.exe `/c $BatchFile”)
}
else
{
Write-host “$computer : Unable to connect”
}
}
Write-Host “Finished execution.Press any key to continue …”
$x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

Configuration Manager 2012: Slow Operating System Deployment (OSD) caused by IPv6 6to4 tunnel adapter

Recently we had an issue with a site where OSD was taking long time on random basis. When I was trying to gather more information about similar issues I come across below article. This article very well describe IP6To4 tunnel adapter and it’s impact in network / AD environment.

http://blogs.technet.com/b/askpfeplat/archive/2013/11/18/ipv6-for-the-windows-administrator-the-2002-6to4-tunnel-address-and-its-impact.aspx

The OSD was extremely slow on random basis. Even few MBs file were taking hours to download. As described in above article, the public IP address was being used in this site as well. The IP 6to4 tunnel adapter was also enabled on SCCM server and client workstations. Once we disabled the IP 6to4 tunnel adapter on SCCM server the build started working fine. We have also disabled this adapter on all workstations via GPO as it may also impact network performance.  It’s over a month now and issue has not been reported back.

Powershell – Retrieve AD Computer Properties

# Retrieve AD computer properties such as LastLogonDate.

# The list of machine need to be added in hosts.txt file in script folder

# The result will be saved to Output.csv file in script folder

Import-Module ActiveDirectory

$invocation = (Get-Variable MyInvocation).Value

$directorypath = Split-Path $invocation.MyCommand.Path

$input = $directorypath + “\host.txt”

$output = $directorypath + “\output.csv”

Get-Content $input | ForEach-Object {

Get-ADComputer $_ -Properties * | Select-Object Name,LastLogonDate,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,WhenCreated,Description

}   | Export-Csv $output

Powershell – List AD Organizational Unit and GPOs linked to them

The below script will search Active Directory for all Organizational Unit which contain specific name and list them along with all Group Policies linked to those OUs.

Example: The below command will get a list of all OUs which name contains ‘Test’. It will also show the details of all GPOs linked to OUs.

Usage Example:

.\Get-OUList.ps1 –OUName “Test”

#Script

[CmdletBinding()]

Param(

[Parameter(Mandatory=$True)]

[string]$OUName

)

$invocation = (Get-Variable MyInvocation).Value

$directorypath = Split-Path $invocation.MyCommand.Path

$outputfile = $directorypath + “\Result.csv”

$OUName = “*” + $OUName + “*”

$Results = @()

$OUList=Get-ADOrganizationalUnit -Filter * | Where-Object -FilterScript {$PSItem.distinguishedname -like $OUName}

foreach($OU in $OUList){

$LinkedGPOs = Get-ADOrganizationalUnit -Identity $OU | select -ExpandProperty LinkedGroupPolicyObjects

 

foreach($LinkedGPO in $LinkedGPOs) {

$GPO = [adsi]”LDAP://$LinkedGPO” | select *

 

$properties = @{

OUName=$OU.DistinguishedName

GPOName=$GPO.displayName.Value

GPOGUID=$GPO.Guid

GPOWhenCreated=$gpo.whenChanged.Value

GPOWhenChanged = $gpo.whenChanged.Value

 

}

$Results += New-Object psobject -Property $properties

}

}

$Results | Select-ObjectOUName,GPOName,GPOGUID,GPOWhenCreated,GPOWhenChanged | Export-Csv -notypeinformation -Path $outputfile