Created
June 12, 2014 12:23
-
-
Save trondhindenes/5249e861eb4bb98f5e15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
workflow set-MITPrestageADComputer | |
{ | |
Param ( | |
[String]$CompName, | |
[string]$OU, | |
$Credential | |
) | |
Write-Output "Prestaging computer $CompName, OU $OU" | |
$verbosepreference = "Continue" | |
#$VesselsDomainCredential = Get-AutomationPSCredential -Name "cred-vesselsdomaincredential" | |
#$VesselsDomainController = Get-AutomationVariable -Name 'var-VesselsDomainDC' | |
$VesselsDomainController = "trondclouddc.trondcloud.hindenes.com" | |
Write-Verbose "Prestaging computer $CompName in trondcloud domain, OU $OU" | |
inlinescript { | |
$CompName = $using:CompName | |
$ou = $using:OU | |
$verbosepreference = "Continue" | |
$ErrorActionPreference = "Stop" | |
if (!$ou) | |
{ | |
Write-error "I didnt get an ou. Breaking." | |
} | |
if (!$CompName) | |
{ | |
Write-error "I didnt get a computer account name. Breaking." | |
} | |
import-module ActiveDirectory -verbose:$false | |
Write-Verbose "Inlinescript: Checking to see if computer $CompName already exists" | |
$computers = get-adcomputer -filter * | |
$computer = $computers | where {$_.Name -eq $CompName} | |
if ($computer) | |
{ | |
Write-verbose "Found computer $($computer.DistinguishedName)" | |
$AssumedDn = "CN=$CompName,$ou" | |
if ($AssumedDn -ne $computer.DistinguishedName) | |
{ | |
Write-Error "Computer account $CompName already exists in target domain, but in the wrong OU. Breaking" | |
} | |
ElseIf ($computer.Enabled -ne $false) | |
{ | |
Write-error "Computer account $CompName already exists in target domain, but the computer account is not disabled. Disable the computer in AD to indicate that it is ready for prestaging" | |
} | |
Else | |
{ | |
Write-verbose "Computer account $CompName already exist and ready for prestaging" | |
} | |
} | |
Else #Computer account not found, creating | |
{ | |
Write-Verbose "Computer $CompName not found, creating" | |
Write-verbose "Creating computer $CompName and setting to disabled" | |
New-ADComputer -Name $CompName -SamAccountName $CompName -Path $ou -enabled $false | |
} | |
} -pscomputername $VesselsDomainController -PSCredential $VesselsDomainCredential | |
Write-Output "set-MITPrestageADComputer runbook completed." | |
} | |
workflow Failedtest | |
{ | |
Param ($cred) | |
set-MITPrestageADComputer -CompName "test" -OU "CN=Computers,DC=trondcloud,DC=hindenes,DC=com" -Credential $cred | |
set-MITPrestageADComputer -CompName "test2" -OU "CN=Computers,DC=trondcloud,DC=hindenes,DC=com" -Credential $cred | |
} | |
workflow Suceedtest | |
{ | |
Param ($cred) | |
set-MITPrestageADComputer -CompName "test" -OU "CN=Computers,DC=trondcloud,DC=hindenes,DC=com" -Credential $cred | |
start-sleep -Seconds 120 | |
set-MITPrestageADComputer -CompName "test2" -OU "CN=Computers,DC=trondcloud,DC=hindenes,DC=com" -Credential $cred | |
} | |
$cred = Get-Credential | |
#This will fail | |
Failedtest -cred $cred | |
#This will suceed | |
Suceedtest -cred $cred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code to demonstrate the PowerShell workflow bug filed to https://connect.microsoft.com/PowerShell/feedback/details/894721/workflows-using-multiple-inlinescripts-with-the-pscomputername-does-not-get-correct-variables-from-the-workflow-when-using-using-scoping