?
Solved

Powershell script

Posted on 2013-12-26
5
Medium Priority
?
639 Views
Last Modified: 2014-01-10
Powershell script to do post sysprep tasks

configure static IP
change server name
add to the domain
activate windows server 2008
0
Comment
Question by:izharkhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39741440
Hi,

here the parts - as I have no demo environment to test, I would (could) not deliver a fully tested script:

1. Configure static IP
Using the WMI
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi.EnableStatic("192.168.24.200", "255.255.255.0")

Open in new window


2. Change Server name
Here you can use the built-in commandlet
http://technet.microsoft.com/en-us/library/hh849792.aspx

Rename-Computer -NewName "NewServer42"

Open in new window


3. Join domain
Here you can use the "Add-Computer" commandlet:
http://technet.microsoft.com/en-us/library/hh849798.aspx

Add-Computer -DomainName "YourDomain" -Credential YourDomain\administrator -OUPath 'ou=YourOU,dc=YourDomain,dc=com'

Open in new window


4. Activate Windows
From this blog post:
http://blogs.technet.com/b/rgullick/archive/2013/06/13/activating-windows-with-powershell.aspx

$computer = gc env:computername
$key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"
$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
$service.InstallProductKey($key)
$service.RefreshLicenseStatus()

Open in new window


HTH
Rainer
0
 

Author Comment

by:izharkhan
ID: 39742033
Hello Rainer,
Thank you so much for your quick response. i have few following questions

(1) For static ip, how can i add gateway and primary and secondary dns. also is there any way that it will pop up for a box to enter values.

(2) For change name, i would like to run as a script that i can enter a name and it will change and reboot the server

(3) For add domain, need script that will ask to enter domain name and credentials.

(4) For activation, it should ask to enter key and then activate the server.


Thanks
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39742922
Hi,

(1) Static IP, gateway and DNS
$newStaticIP = Read-Host 'Please enter the static IP'
$newStaticSubnet = Read-Host 'Please enter the subnet mask'
$newGateway = Read-Host 'Please enter the gateway'
$newDNS = Read-Host 'Please enter the DNS server IP'
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi.EnableStatic($newStaticIP, $newStaticSubnet)
$wmi.SetGateways($newGateway, 1)
$wmi.SetDNSServerSearchOrder($newDNS)

Open in new window


(2) Server name and reboot
$newComputerName = Read-Host 'Please enter the new computer name'
Rename-Computer -NewName $newComputerName
Write-Host "Local computer will be now rebooted"
Restart-Computer

Open in new window


(3) Domain join with credentials
$username = Read-Host 'Please enter user name (domain\user)'
$pass = Read-Host 'Please enter the password' -AsSecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$pass
$domainToJoin = Read-Host 'Please enter the domain to join'
Add-Computer -DomainName $domainToJoin -Credential $Cred

Open in new window


(4) License
$computer = gc env:computername
$key = Read-Host 'Please enter the license key (including the hyphens)'
$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
$service.InstallProductKey($key)
$service.RefreshLicenseStatus()

Open in new window


HTH
Rainer
0
 

Author Comment

by:izharkhan
ID: 39744307
Hello Rainer,
i checked all the scripts and it work fine. is there any way that you can create one script and do manu to run all these scripts?
example:   press   1. static ip, 2. change name .....
That will be great.


thanks Genius :)
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 39744518
Hi,
here we go:
[int]$menuChoice = 0
while ( $menuChoice -lt 1 -or $menuChoice -gt 5 ){
	Write-host "1. Static IP"
	Write-host "2. Rename computer"
	Write-host "3. Join domain"
	Write-host "4. Activate Windows"
	Write-host "5. Quit and exit"
	[Int]$menuChoice = read-host "Please enter an option 1 to 5..." 
}
Switch( $menuChoice ){
  1{
		$newStaticIP = Read-Host 'Please enter the static IP'
		$newStaticSubnet = Read-Host 'Please enter the subnet mask'
		$newGateway = Read-Host 'Please enter the gateway'
		$newDNS = Read-Host 'Please enter the DNS server IP'
		$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
		$wmi.EnableStatic($newStaticIP, $newStaticSubnet)
		$wmi.SetGateways($newGateway, 1)
		$wmi.SetDNSServerSearchOrder($newDNS)
  }
  2{
		$newComputerName = Read-Host 'Please enter the new computer name'
		Rename-Computer -NewName $newComputerName
		Write-Host "Local computer will be now rebooted"
		Restart-Computer
  }
  3{
		$username = Read-Host 'Please enter user name (domain\user)'
		$pass = Read-Host 'Please enter the password' -AsSecureString
		$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$pass
		$domainToJoin = Read-Host 'Please enter the domain to join'
		Add-Computer -DomainName $domainToJoin -Credential $Cred  
  }
  4 {
		$computer = gc env:computername
		$key = Read-Host 'Please enter the license key (including the hyphens)'
		$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
		$service.InstallProductKey($key)
		$service.RefreshLicenseStatus()  
  }
default{Exit-PSSession}
}

Open in new window


HTH and happy new year
Rainer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question