0

So I made this script to find the next avaible script in a series. The computer will get a name with ComputerXXXX (XXXX being 0-9999). It works when I run it manually after the TS is done, but not when I run it through TS as a step. Any tips? It does print the information into the .csv file so the script does run. Have been reading about OSDComputername or something, any info on this?

# Variables debugging  
$global:navneserie = $null  
$global:navneserieNavn = $null  
$computerName = $env:COMPUTERNAME  
$Dato = (get-date)  
$log = "Enabled"  
write-host $computername  
import-module ActiveDirectory  
write-host $serialnumber  


$global:navneserie = "Computer"  
$global:navneserieNavn = "Computer"  

function SCCMLog  
{  
    if ($log -eq "Enabled")
    {  
        add-content \\DOMAINNAME\NETLOGON\SCCMLog\SCCMlog.csv "$Dato;$Computername; ;OK"  
    }  
}  

if ($env:computerName -like "Computer*")  
{  
    write-host $computername  
    exit  
}  

else   
{  
    $OpptatteNavn = Get-ADComputer -properties name -filter *| where {$_.name -match $global:navneserie} | ForEach-Object {$_.Name} | sort  

    [int]$forrige = 9999  
    [int]$denne = 0000  
    $funn = $false  


While (-not $funn)  
{  
    Foreach ($navn in $OpptatteNavn)  
    {  
        $navn = $navn.substring($navn.length -4,4)  
        $denne = $navn  
        If ((($denne-$forrige) -gt 1)-and -not $funn)  
        {  
            [int]$nesteLedige = $forrige + 1  
            $funn = $true  
            break  
        }  
        $forrige = $navn  
    }  
}  
If ($nesteLedige -lt 10)  
{  
    $000Name = $global:navneserieNavn+"000"+$nesteLedige  
    rename-computer -newname "$000Name"  
    write-host "Datamaskinen har blitt renamet til:"$global:navneserieNavn"000"$nesteLedige"`n"  
    LogSCCM  
}  
elseIf ($nesteLedige -lt 100)  
{  
    $00Name = $global:navneserieNavn+"00"+$nesteLedige
    rename-computer -newname "$00Name"
    write-host "Datamaskinen har blitt renamet til:"$global:navneserieNavn"00"$nesteLedige"`n"
    LogSCCM
}
elseIf ($nesteLedige -lt 1000)
{
    $0Name = $global:navneserieNavn+"0"+$nesteLedige
    rename-computer -newname "$0Name"
    write-host "Datamaskinen har blitt renamet til:"$global:navneserieNavn"0"$nesteLedige
    LogSCCM
}
else
{
    rename-computer = $global:navneserieNavN$nesteLedige
    write-host "Datamaskinen har blitt renamet til:$global:navneserieNavn$nesteLedige"
    LogSCCM
}
J3STER
  • 1,027
  • 2
  • 11
  • 28
Asphaug
  • 21
  • 1
  • 7
  • 1
    You can use the OSDComputername variable to set the computer name, while running the Task Sequence, something like this: $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment $TSEnv.Value("OSDComputername")='yournamehere' – Narcis Mar 23 '17 at 20:00
  • Replaced "rename-computer -newname "$000Name"" with "$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment $TSEnv.Value("OSDComputername")='yournamehere'" Now the TS is stuck at the step "Run powershell script" Does not seem to print anything to the log file either. – Asphaug Mar 24 '17 at 07:52
  • These are on two lines... just in case the lack of formatting from the comment field was misleading. Line1 -> $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment Line2 -> $TSEnv.Value("OSDComputerName")='yournamehere' – Narcis Mar 24 '17 at 13:25
  • Thank you, still no master with powershell. Atleast not with sort of use. Will be testing this on monday and give you a feedback. – Asphaug Mar 24 '17 at 16:26
  • Just occurred to me that if you are using the Rename-Computer cmdlet, then you are running this script in the Full OS and not in the WinPE phase. For this change of the OSDComputerName variable to work, it has to be performed before the Full OS is in place, as this variable is picked up by the Unattended configuration during the initial Sysprep performed during the step "Setup Windows and ConfigMgr". – Narcis Mar 24 '17 at 16:48
  • Hey, i tried having the step between "Apply operating system" and "Apply windows settings". Does not seem to work. For some reason i does not write anything to the log anymore either. I replaced 'yournamehere' with my variable. But am i supposted to remove the ' ? – Asphaug Mar 27 '17 at 10:42
  • Yes, remove the single quotes as they will prevent your variable substitution. You can use double quotes or none. – Narcis Mar 27 '17 at 18:29
  • I got somthing working. When i manually say $Newname = "Test123" and then -> $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment $TSEnv.Value("OSDComputername")="$Newname" i actually get the name "Test123". But when i go through AD to find the next avaible name it stops in the TS. no error nothing. When i run the script on any other machine and replace the rename part with just "write-host $newname" where $newname is the new name imported from AD it prints the right name. Any tips? – Asphaug Mar 29 '17 at 08:57
  • Got it working, found out that the computer is not in the domain before after PE. So i had to manually say what domain to check through in the script. Thank you. – Asphaug Mar 29 '17 at 13:32
  • Since you knew a bit about OSDComputername, in the start of the script, how can i check what the computername is? "$env:computername" did not work. I guess i need to use the OSDComputername again somehow? – Asphaug Mar 29 '17 at 13:33
  • To get the current hostname for the machine, during WindowsPE phase, use $TSEnv.Value("_SMSTSMachineName"), after $TSEnv being initialized in the same way as above ($TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment). This one is a read-only variable as it starts with _. – Narcis Mar 30 '17 at 06:50

0 Answers0