Ying Li at myITforum.com

PowerShell & System Center

September 2008 - Posts

PowerShell script to delete sub folders old than certain days

In my previous Blog, I discussed how to use PowerShell script to delete files old than certain days. At the time I was focused on the files (log files in particular). Recently there is a need to do the similar thing for folders like if you have Backup folder contain sub backup folders created on different date. Here is a modified script to get the job done.

Function RemoveOldFile
{
param ($strComputer = $(Read-Host "Please Enter The Server Name")),
       ($Dir = $(Read-host "Please Enter The Directory Path"))
       ($Days = $(Read-Host "How Many Days?"))

$TargetFolder = "\\" + $strComputer + "\" + $Dir
if (Test-Path $TargetFolder)
{

#Warn you the targeted folder, so you can double check
  Write-host "The Targeted Folder is:" $TargetFolder -foregroundcolor "Red"
  Write-Host `a `a `a `a `a
  Write-Host "If This Is Not The Intended Target, Press 'Ctrl + C' To Exit" -foregroundcolor "Yellow"
  Start-sleep -s 15

  $Now = Get-Date

# Notice the minus sign before $days
  $LastWrite = $Now.AddDays(-$days)
  Get-ChildItem $Targetfolder |Where {$_.LastWriteTime -le "$LastWrite"}|remove-item -recurse
}
Else
{Write-Host "The Folder $TargetFolder Does Not Exist!"}
}
RemoveOldFile

Enjoy!

PowerShell script to identify what vm machines are on a particular LUN

We have a VMWare infrastructure which connects to the SAN. Every now and then we will get "running out disk space" error for a particular LUN. What we need to do is to identify what other machines are on the same LUN, then we can address the disk space issue accordingly. Here is a PowerShell one liner to accomplish this and of course you need to have VMWare VI ToolKit installed.

Get-ESX # to connect to the target ESX box

Get-vm -datastore "lunxy"

PowerShell script to create AD accounts

If you keep getting request to create multiple (service) accounts in AD, you got to think what PowerShell can do for this task. Before Microsoft releases it's own AD Cmdlets, we will have to use PowerShell commands for Active Directory from quest. You can download the latest version Here.

After you get it installed, you can start a regular PowerShell session and type:

Add-PSSnapIn Quest.ActiveRoles.ADManagement

This will extend the PowerShell for AD

First we save the plaintext password as a AsSecureString

PS C:\Users\yl.admin\Documents\PS> $pw = read-host "Enter password" -AsSecureString
Enter password: ********

Then connect to the targeted domain

PS C:\Users\yl.admin\Documents\PS> Connect-QADService -service 'xyzdcs01.xyz-stage.com' -ConnectionAccount 'xyz-stage\administrator' -ConnectionPassword $pw

Once you establish the connection to AD, you can import the csv file which has the below format:

Name            Description
Svc_SP_IntrAP        Intranet content web application pool
Svc_SP_IntrSSPAP    Intranet farm shared services provider application pool

PS C:\Users\yl.admin\Documents\PS> import-csv C:\myworkspace\user.csv |%{new-qadUser -ParentContainer 'OU=Service Ac
ts,DC=xyz-stage,DC=com' -name $_.name -samAccountName $_.name -description $_.Description}

You will see the list of accounts created after you run the above script.

PowerShell script to push BITS 2.5 to remote computers

After we got a list of computers need BITS 2.5 discussed in my previous Post. We could use the below script to push BITS 2.5 to them

$colComputers = gc c:\users\yl.admin\pstools\ComputerList.txt
Foreach ($strComputer in $colComputers)

{.\psexec.exe -c \\$strComputer \\server\share\WindowsXP-KB923845-x86-ENU.exe /quiet /norestart}

You will see something like this which is perfectly fine and it just means reboot is required and pending!

WindowsXP-KB923845-x86-ENU.exe exited on $strcomputer with error code 3010.

PowerShell script to check BITS version

BITS 2.5 is a required component by ConfigMgr 2007. The ccmsetup will automatically install BITS 2.5 if the client doesn't have it but there is a little catch - it will reboot the machine! I have been trying to find a way to suppress the reboot and so far without success.

It seem the only way to control the reboot is to pre-install BITS 2.5. Before I do that, here is a PowerShell script to query BITS version on multiple remote computers.

$erroractionpreference = "SilentlyContinue"

$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Ping Status"
$c.Cells.Item(1,3) = "File Name"
$c.Cells.Item(1,4) = "Version"
$c.Cells.Item(1,5) = "Report Time Stamp"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$intRow = 2

$colComputers = gc c:\myworkspace\MachineList.txt

foreach ($strComputer in $colComputers)
{
$c.Cells.Item($intRow,1) = $strComputer.ToUpper()

Function PingComputer
{
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer)
if($Reply.status –eq “Success”)
{
$c.Cells.Item($intRow, 2) = “Online”

Function GetFileInfo
{
$OSVersion = (gwmi -class Win32_OperatingSystem -computer $strComputer).version
if ($OSVersion -le 5.1)
{
$Path = "\\"+ $strComputer + "\C$\Winnt\System32\qmgr.dll"
}
else
{
$Path = "\\"+ $strComputer + "\C$\Windows\System32\qmgr.dll"
}

$File = get-item $Path

$c.Cells.Item($intRow,3) = $File.Name
$c.Cells.Item($intRow,4) = $File.VersionInfo.Productversion
}

GetFileInfo

}
else
{
$c.Cells.Item($intRow, 2).Interior.ColorIndex = 3
$c.Cells.Item($intRow, 2) = "Offline"
}
}
PingComputer

$c.Cells.Item($intRow,5) = Get-date

$ping.status = $null
$intRow = $intRow + 1
}

$d.EntireColumn.AutoFit()

Setup ConfigMgr SP1 on Windows Server 2008

In my previous post, I described how to get your ConfigMgr SP1 environment up and running in no time. Now here is a update version for how to set up ConfigMgr SP1 environment in Windows Server 2008. Enjoy!

Download the doc Here

How to join a client to a domain behind a NAT environment

We have a development domain setup inside (VMWare) Labmanager which is "isolated" from outside. Each machine has it's private IP address such as 10.10.10.x and also has an "external" IP Address such as 172.16.48.x. There is a need to add machine external to labmanager which has 172.16.48.* address to this dev domain.

When I try to join a test XP machine to the domain I receive the following message:

Note: This information is intended for a network administrator. If you are not your network's administrator, notify the administrator that you received this information, which has been recorded in the file C:\WINDOWS\debug\dcdiag.txt.

The following error occurred when DNS was queried for the service location (SRV) resource record used to locate a domain controller for domain xyz-dev.com:

The error was: "This operation returned because the timeout period expired."
(error code 0x000005B4 ERROR_TIMEOUT)

The query was for the SRV record for _ldap._tcp.dc._msdcs.xyz-dev.com

The DNS servers used by this computer for name resolution are not responding. This computer is configured to use DNS servers with the following IP addresses:

172.16.48.10

Verify that this computer is connected to the network, that these are the correct DNS server IP addresses, and that at least one of the DNS servers is running.

For more information on how to correct this problem, click Help.

172.16.48.10 is the "external" IP address for the domain controller/DNS server in dev domain

In order to correct this error, I added the following to the host file on the XP test machine

172.16.48.10             xyz-dev.com

The I see some progress and got a different error:

DNS was successfully queried for the service location (SRV) resource record used to locate a domain controller for domain xyz-dev.com:

The query was for the SRV record for _ldap._tcp.dc._msdcs.xyz-dev.com

The following domain controllers were identified by the query:

xyzdcd01.xyz-dev.com

Common causes of this error include:

- Host (A) records that map the name of the domain controller to its IP addresses are missing or contain incorrect addresses.

- Domain controllers registered in DNS are not connected to the network or are not running.

For information about correcting this problem, click Help.

Even after I added the below entry in the host file - I still get the same error messag:

172.16.48.10                xyzdcd01.xyz-dev.com

After give it some thought. I believe the problem is because the test machine can resolve to xyz-dev.com through the first entry in the host file

172.16.48.10      xyz-dev.com

it queries the SRV record and got the domain controller name which is xyzdcd01.xyz-dev.com - all happy so far!

but the IP address the test machine got for the DC is not 172.16.48.10 but rather the internal IP address of the DC/DNS box which happens to be 10.10.10.10 which the test box has no way to connect to and the second entry in host file

172.16.48.10        xyzdcd01.xyz-dev.com doesn't help at all!

I tried to create a new Host(A) record for the DC let the xyzdcd01.xyz-dev.com has two A record one is point to internal IP address 10.10.10.10 and the other point to external IP address 172.16.48.10 and I can then add the test xp machine to the domain. But before I got too excited about this - I realizes and noticed that I can't do that, the DNS server automatically delete the "external" A record for the DC after a while!

Then I went back to read the above error message and focus my attention on:

_ldap._tcp.dc._msdcs.xyz-dev.com

I then go to the DNS server and modify the above pointer to the external IP address 172.16.48.10 (it had the internal IP address by default)!

Now the "external" machines can join the dev domain and the machine inside the labmanagers can still join the dev domain because they each has an external IP address and they can go out (they are setup to use the external getaway) to join the domain.

After that I created a secondary DNS zone on my "external" production DNS server which now hold the zone information for xyz-dev, so I don't have to add the xyz-dev.com host file entry. All I need to do is to let the client using the external IP address of the dev DNS server as it's preferred DNS server IP address if I need to add that client to the dev domain.

I spent quite some time on this and figure to share them but keep in mind, this may not be an officially supported scenario and may have unintended consequences!