Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - function call from within powershell script

Reply
 
Old 08-07-2007   #1 (permalink)
Anatoli


 
 

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 SpecsSystem Spec
Old 08-08-2007   #2 (permalink)
Oisin Grehan


 
 

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 SpecsSystem Spec
Old 08-08-2007   #3 (permalink)
Marco Shaw


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46