![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | function call from within powershell script Hi, Being new to scripting as a whole, I want to create a function from within my script below that will run the following snippet of code that I have to repeat.. if ($strSamAccount.samaccountname -ne $strUserName) { Write-Host -noNewLine " Creating Windows Account please wait." -foregroundcolor green for ($i=0;$i -le 50; $i++) {if ($i -lt 50) {Write-Host -noNewLine "." -foregroundcolor green Start-Sleep -m 50} else {Write-Host "." -foregroundcolor green} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail -mustchpwd yes -memberof $strGroup } Which variables would be global and which would be local? Also, how do I declare one to be global and the other to be local to the function? ________________________________________________________________ $strFirstName = "Josepe" $strMiddleInitial = "W" $strLastName = "Schmoe" $strUserName = $strFirstName.Substring(0,1) + $strLastName $strDisplayName = $strFirstName + " " + $strMiddleInitial + ". " + $strLastName $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" $strUpn = $strUserName + "@ham.sitel.co.nz" $strSamid = $strUserName $strGroup = "CN=MyNewGroup,OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz" $strCNName = "OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz" $strValue = "CN="+ $strDisplayName + "," + $strCNName $strSamAccount = get-adobject -value $strUserName if ($strSamAccount.samaccountname -ne $strUserName) { Write-Host -noNewLine " Creating Windows Account please wait." -foregroundcolor green for ($i=0;$i -le 50; $i++) {if ($i -lt 50) {Write-Host -noNewLine "." -foregroundcolor green Start-Sleep -m 50} else {Write-Host "." -foregroundcolor green} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail -mustchpwd yes -memberof $strGroup } else { $strUserName = $strFirstName.Substring(0,2) + $strLastName $strSamAccount = get-adobject -value $strUserName $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" $strSamid = $strUserName $strUpn = $strUserName + "@ham.sitel.co.nz" if ($strSamAccount.samaccountname -ne $strUserName) { Write-Host -noNewLine "Creating Windows Account please wait." -foregroundcolor green for ($i=0;$i -le 50; $i++) {if ($i -lt 50) {Write-Host -noNewLine "." -foregroundcolor green Start-Sleep -m 50} else {Write-Host "." -foregroundcolor green} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail -mustchpwd yes -memberof $strGroup } else { $strUserName = $strFirstName.Substring(0,3) + $strLastName $strSamAccount = get-adobject -value $strUserName $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" $strSamid = $strUserName $strUpn = $strUserName + "@ham.sitel.co.nz" if ($strSamAccount.samaccountname -ne $strUserName) { Write-Host -noNewLine " Creating Windows Account please wait." -foregroundcolor green for ($i=0;$i -le 50; $i++) {if ($i -lt 50) {Write-Host -noNewLine "." -foregroundcolor green Start-Sleep -m 50} else {Write-Host "." -foregroundcolor green} } dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail -mustchpwd yes -memberof $strGroup } else { Write-Host "There is an issue of duplicate usernames. Please contact IT to create windows account!" -foregroundcolor red } } } |
My System Specs![]() |
| | #2 (permalink) |
| | Re: function call from within powershell script On Aug 7, 11:42 pm, Anatoli <Anat...@discussions.microsoft.com> wrote: > Hi, > > Being new to scripting as a whole, I want to create a function from within > my script below that will run the following snippet of code that I have to > repeat.. > > if ($strSamAccount.samaccountname -ne $strUserName) > { > Write-Host -noNewLine " Creating Windows Account please wait." > -foregroundcolor green > for ($i=0;$i -le 50; $i++) > {if ($i -lt 50) > {Write-Host -noNewLine "." -foregroundcolor green > Start-Sleep -m 50} > else > {Write-Host "." -foregroundcolor green} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi > $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail > -mustchpwd yes -memberof $strGroup > } > > Which variables would be global and which would be local? Also, how do I > declare one to be global and the other to be local to the function? > > ________________________________________________________________ > $strFirstName = "Josepe" > $strMiddleInitial = "W" > $strLastName = "Schmoe" > $strUserName = $strFirstName.Substring(0,1) + $strLastName > $strDisplayName = $strFirstName + " " + $strMiddleInitial + ". " + > $strLastName > $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" > $strUpn = $strUserName + "@ham.sitel.co.nz" > $strSamid = $strUserName > $strGroup = "CN=MyNewGroup,OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz" > $strCNName = "OU=MyTestOU,DC=ham,DC=sitel,DC=co,DC=nz" > $strValue = "CN="+ $strDisplayName + "," + $strCNName > > $strSamAccount = get-adobject -value $strUserName > if ($strSamAccount.samaccountname -ne $strUserName) > { > Write-Host -noNewLine " Creating Windows Account please wait." > -foregroundcolor green > for ($i=0;$i -le 50; $i++) > {if ($i -lt 50) > {Write-Host -noNewLine "." -foregroundcolor green > Start-Sleep -m 50} > else > {Write-Host "." -foregroundcolor green} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn $strFirstName -mi > $strMiddleInitial -ln $strLastName -display $strDisplayName -email $strEmail > -mustchpwd yes -memberof $strGroup > } > else > { > $strUserName = $strFirstName.Substring(0,2) + $strLastName > $strSamAccount = get-adobject -value $strUserName > $strEmail = $strFirstName + "." + $strLastName + "@ham.sitel.co.nz" > $strSamid = $strUserName > $strUpn = $strUserName + "@ham.sitel.co.nz" > if ($strSamAccount.samaccountname -ne $strUserName) > { > Write-Host -noNewLine "Creating Windows Account please wait." > -foregroundcolor green > for ($i=0;$i -le 50; $i++) > {if ($i -lt 50) > {Write-Host -noNewLine "." -foregroundcolor green > Start-Sleep -m 50} > else > {Write-Host "." -foregroundcolor green} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn > $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName > -email $strEmail -mustchpwd yes -memberof $strGroup > } > else > { > $strUserName = $strFirstName.Substring(0,3) + $strLastName > $strSamAccount = get-adobject -value $strUserName > $strEmail = $strFirstName + "." + $strLastName + > "@ham.sitel.co.nz" > $strSamid = $strUserName > $strUpn = $strUserName + "@ham.sitel.co.nz" > if ($strSamAccount.samaccountname -ne $strUserName) > { > Write-Host -noNewLine " Creating Windows Account please > wait." -foregroundcolor green > for ($i=0;$i -le 50; $i++) > {if ($i -lt 50) > {Write-Host -noNewLine "." -foregroundcolor green > Start-Sleep -m 50} > else > {Write-Host "." -foregroundcolor green} > } > dsadd user $strValue -upn $strUpn -samid $strSamid -fn > $strFirstName -mi $strMiddleInitial -ln $strLastName -display $strDisplayName > -email $strEmail -mustchpwd yes -memberof $strGroup > } > else > { > Write-Host "There is an issue of duplicate usernames. Please > contact IT to create windows account!" -foregroundcolor red > } > } > } Without having a whole load of time to analyze your script, you could do a lot worse than sitting through a few webcasts; you could try viewing some of Don's beginnner webcasts to get a better handle on scripting in general. http://www.microsoft.com/events/seri...scripting.aspx - Oisin |
My System Specs![]() |
| | #3 (permalink) |
| | Re: function call from within powershell script > Without having a whole load of time to analyze your script, you could > do a lot worse than sitting through a few webcasts; you could try > viewing some of Don's beginnner webcasts to get a better handle on > scripting in general. > > http://www.microsoft.com/events/seri...scripting.aspx > > - Oisin > For something specific to scopes in PowerShell, I seem to remember Don only skimming the surface in this particular webcast from that link above: "TechNet Webcast: Functions, Filters, and Efficiency in Windows PowerShell (Level 200)" Check out 'get-help about_scope' if you haven't already. Marco |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Call binary to return value in Powershell script | PowerShell | |||
| How to call PowerShell script from C# | PowerShell | |||
| Powershell Wrapper Script Problems - Trying to Call PowershellExchange 2007 Commands from Secondary Language (Like VBScript) | PowerShell | |||
| Call powershell script via Batch-File | PowerShell | |||
| Call Powershell script from ASP.NET page? | PowerShell | |||