try to create a function for my messy script
-
Friday, December 14, 2012 7:13 AM
Hello,
I am trying to make a function so that my messy script will looks better. I thought I know how but reality is not even close. Please help so that I can learn from my script.
# Host name is base on 2 different format 8char and 12char: eg: AAENGI11, ABAAENGXD123 # this script is for # 1. create an user account in AD base on local host # 2. make autologon in registry $name = $env:computername if(($name.length -eq 8) -or ($name.length -eq 12)){ if( $name.length -eq 8 ) { $dept=$env:computername.substring(0,6) $dig=$env:computername.substring(7,2) $logon="AL"+$dept+"0"+$dig $password = 'xxxxx' #ADD TO REGISTRY $hklm = "hklm:\software\microsoft\windows nt\currentversion\winlogon" Set-ItemProperty -Path $hklm -Name AutoAdminLogon -Value 1 Set-ItemProperty -Path $hklm -Name DefaultUserName -Value $logon Set-ItemProperty -Path $hklm -Name DefaultDomainName -Value xxxx Set-ItemProperty -Path $hklm -Name DefaultPassword -Value 'xxxx' Set-Itemproperty -Path $hklm -Name ForceAutoLogon -Value 1 #CREATE AD ACCOUNT $ou = "OU=Auto Logon Generic Users,OU=Users,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" New-QADUser -Name $logonlowercase -ParentContainer $ou -description "Auto Log on User $name" -DisplayName $logonlowercase -UserPrincipalName $logonlowercase -SamAccountName $logonlowercase -UserPassword "$password" | Set-QADUser -PasswordNeverExpires $true #ADD TO GROUPS $epic_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $gdrive_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $abuser_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $ebr_credant_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" Add-QADGroupMember -Identity $epic_group -Member $logon Add-QADGroupMember -Identity $gdrive_group -Member $logon Add-QADGroupMember -Identity $abuser_group -Member $logon Add-QADGroupMember -Identity $ebr_credant_group -Member $logon } if ($name.length -eq 12) { $dept=$env:computername.substring(2,5) $dig=$env:computername.substring(9,3) $logon="AL"+$dept+$dig $logonlowercase = $logon.ToLower()
$password = xxxxxx $hklm = "hklm:\software\microsoft\windows nt\currentversion\winlogon" Set-ItemProperty -Path $hklm -Name AutoAdminLogon -Value 1 Set-ItemProperty -Path $hklm -Name AltDefaultUserName -Value $logon Set-ItemProperty -Path $hklm -Name DefaultUserName -Value $logon Set-ItemProperty -Path $hklm -Name DefaultDomainName -Value xxxx Set-ItemProperty -Path $hklm -Name DefaultPassword -Value 'xxxxxx' Set-Itemproperty -Path $hklm -Name ForceAutoLogon -Value 1 #CREATE AD ACCOUNT $ou = "OU=Auto Logon Generic Users,OU=Users,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" New-QADUser -Name $logonlowercase -ParentContainer $ou -description "Auto Log on User $name" -DisplayName $logonlowercase -UserPrincipalName $logonlowercase -SamAccountName $logonlowercase -UserPassword "$password" | Set-QADUser -PasswordNeverExpires $true #ADD TO GROUPS $epic_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $gdrive_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $abuser_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" $ebr_credant_group = "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" Add-QADGroupMember -Identity $epic_group -Member $logon Add-QADGroupMember -Identity $gdrive_group -Member $logon Add-QADGroupMember -Identity $abuser_group -Member $logon Add-QADGroupMember -Identity $ebr_credant_group -Member $logon } cls } else {write-host "Name must be 8 or 12 charactor.... Please call x2222"}
- Edited by Paschal L Friday, December 14, 2012 7:32 AM
All Replies
-
Friday, December 14, 2012 8:42 AM
function Set-StandardData { [cmdletbinding()] Param( $name = $env:computername ) if( $name.length -eq 8 ) { $dept=$env:computername.substring(0,6) $dig=$env:computername.substring(7,2) $logon="AL"+$dept+"0"+$dig $password = 'xxxxx' } elseif ($name.length -eq 12) { $dept=$env:computername.substring(2,5) $dig=$env:computername.substring(9,3) $logon="AL"+$dept+$dig } else{write-host "Name must be 8 or 12 charactor.... Please call x2222"
return} $logonlowercase = $logon.ToLower() #ADD TO REGISTRY $hklm = "hklm:\software\microsoft\windows nt\currentversion\winlogon" $list = @{ AutoAdminLogon = 1 AltDefaultUserName = $logon DefaultUserName = $logon DefaultDomainName = xxxx DefaultPassword = $password ForceAutoLogon = 1 } $list.GetEnumerator() | ForEach-Object{ Set-ItemProperty -Path $hklm -Name $_.Key -Value $_.Value } #CREATE AD ACCOUNT $ou = "OU=Auto Logon Generic Users,OU=Users,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" New-QADUser -Name $logonlowercase -ParentContainer $ou -description "Auto Log on User $name" -DisplayName $logonlowercase -UserPrincipalName $logonlowercase -SamAccountName $logonlowercase -UserPassword "$password" | Set-QADUser -PasswordNeverExpires $true #ADD TO GROUPS "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX", "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX", "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX", "CN=XXX,OU=XXX,OU=XXXX,DC=XXX,DC=XXXX,DC=XXX" | ForEach-Object { Add-QADGroupMember -Identity $_ -Member $logon } cls}
how about that?
- Edited by BlindroodMicrosoft Community Contributor Friday, December 14, 2012 10:10 AM
-
Friday, December 14, 2012 10:01 PM
Blindood: thank you for your reply.
I am not quite understand what [cmdletbinding()] is doing. I can still run without it. Also didn't see the function set-standarData calling in the script either.
If you could brift explain, really appriciate it. Thank you for point out the elself statement and others funtion. lots of things to learn from it.
thank you.
-
Saturday, December 15, 2012 11:22 AM
about cmdletbinding:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/07/weekend-scripter-cmdletbinding-attribute-simplifies-powershell-functions.aspx
You wanted a function, not a script. To make it a script remove function wrapper (function set-standarddata { })
and leave only what's inside. Save it as Set-StandardData.ps1.
Then You can execute it by:
.\Set-StandardData.ps1
or
.\Set-StandardData.ps1 -name somepcname
- Marked As Answer by Paschal L Saturday, December 15, 2012 1:45 PM
-
Saturday, December 15, 2012 1:45 PM
got it.
Thank You.

