Ensign
Ensign
1524 views

WinPE Imaging - Compuer Name Variable Function

We are on ZENworks 2017 2a and I am working on a Powershell preboot script for WinPE imaging that will check if the computer has a name in image safe data. If it does then it will ask to keep the existing name or allow the user to enter a new one. If not, then it will prompt "No image safe data found" and it will require a name. I mirrored logic that we have been using for years in the Linux environment and it worked fine for all cases. However that logic is not working in WinPE using Powershell. Here is the code I am using (see code in blue since this is where the problem is):

$ZISNAME= zisview ComputerName

If (!$ZISNAME)

{
Do
{
Write-Host ""
Write-Host "No Zenworks image-safe data found. A workstation name is required to continue." -ForegroundColor White -BackgroundColor Red
Write-Host ""
$WSNAME = Read-Host -Prompt "Enter the workstation name"
Get-Variable WSNAME -ValueOnly
if($WSNAME)
{
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
#zisedit netbiosName=$WSNAME #Not needed, changed by ComputerName
$ZISNAME= $WSNAME
Write-Host "The workstation name is $ZISNAME"
Write-Host ""
}

elseif (!$WSNAME)
{
$ZISNAME= $null
}

}
While(!$ZISNAME)
}


ElseIf ($ZISNAME)
{
Write-Host ""
Write-Host ""
Write-Host "Computer Name: $ZISNAME"
Write-Host ""
$WSNAME = Read-Host -Prompt "Change Name (Enter Name to change,press Enter to keep existing)"
Get-Variable WSNAME -ValueOnly
if ($WSNAME)
{
Write-Host "Name Changed to $WSNAME"
Write-Host ""
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
$ZISNAME= $WSNAME
}
}



If I use If(!ZISNAME) then it doesn't account for no image safe data. It shows computer name is: <blank> then asks to enter a new name. So if I run zisedit -c then run the script, it thinks no data is a name because the value is not null. If I set ZISNAME to $null at the beginning of the script or enter any value then the script works fine.

If I use If(ZISNAME= " ") then it does not accept a new name but keeps whatever name is entered and continues on with the script. There is a space between the quotes.

If I use If(ZISNAME="") then it skips asking for a name and goes right to installing the image.

So using !ZISNAME would be the best option but I don't know how to account for a blank name since it is technically not null. Any ideas?
Labels (1)
0 Likes
8 Replies
Micro Focus Expert
Micro Focus Expert

I did some basic testing.....
It appears that the "LENGTH" for a "Blank" computer name is 2 Characters....
Try setting your script to look for the LENGTH to be > 2.......Assuming you would never have an 1 or 2 character computer names.....
If it is >2 then you do not need to prompt but if 2 or less then you need to prompt..........


https://stackoverflow.com/questions/16262403/how-can-i-test-that-a-variable-is-more-than-eight-characters-in-powershell
http://brilliantlyeasy.com/powershell-count-character-in-string-variable/
--
Please give a hearty thumbs up to any post you find helpful!
To find articles by Craig Wilson simply follow the link: Craig Wilson's Tips!
0 Likes
Micro Focus Expert
Micro Focus Expert

Note: I have no clue what those "2" characters may be....
Any substring operation fails trying to pull out the 1st or 2nd character to get it's UNICODE value fails....
the same operations work if i set a var to something such as "ab"
--
Please give a hearty thumbs up to any post you find helpful!
To find articles by Craig Wilson simply follow the link: Craig Wilson's Tips!
0 Likes
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

Not sure but for me if i did this:

[string] $ZISNAME= .\zisview.exe ComputerName

it ended up being a space " " character, but not sure if that's expected or same for you.
0 Likes
Ensign
Ensign

Thank you Craig and sperrin for your suggestions. When I checked for length <=2 and had it prompt for no image safe data it worked fine when I ran zisedit -c beforehand to clear the data to simulate a new drive, or a computer with no image safe data. So that worked as intended. Yet when I ran the script again with an existing name (and I ran zisview ComputeName to verify the name existed) the script said no name existed and prompted for a name. When I tested this in Powershell ISE and I set the name to a value (12345-LT, or $null or two spaces, the script worked fine. So I was surprised it did not work in the Powershell script running in the WinPE environment. Here is what I changed the script to:

$ZISNAME= zisview ComputerName

If ($ZISNAME.length -le 2)
{
Do
{
Write-Host ""
Write-Host "No Zenworks image-safe data found. A workstation name is required to continue." -ForegroundColor White -BackgroundColor Red
Write-Host ""
$WSNAME = Read-Host -Prompt "Enter the workstation name"
Get-Variable WSNAME -ValueOnly
if($WSNAME)
{
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
$ZISNAME= $WSNAME
Write-Host "The workstation name is $ZISNAME"
Write-Host ""
}

elseif ($WSNAME.length -le 2)
{
$ZISNAME.length -eq 2
}


}
While($ZISNAME.length -le 2)
}

ElseIf ($ZISNAME.length -gt 2)

{
Write-Host ""
Write-Host ""
Write-Host "Computer Name: $ZISNAME"
Write-Host ""
$WSNAME = Read-Host -Prompt "Change Name (Enter Name to change, press Enter to keep existing)"
Get-Variable WSNAME -ValueOnly
if ($WSNAME)
{
Write-Host "Name Changed to $WSNAME"
Write-Host ""
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
$ZISNAME= $WSNAME
}
}
0 Likes
Ensign
Ensign

I ran the following commands in Powershell in WinPE:
zisview ComputerName
Output: PCTECH-E5440
$ZISVIEW= zisview ComputerName
$x = $ZISVIEW.length
Write-Host "Length $x"
Length 0

So $ZISVIEW.length is not calculating the length correctly.
0 Likes
Ensign
Ensign

I needed to use the "hard way" as described in this link from Craig:

http://brilliantlyeasy.com/powershell-count-character-in-string-variable/

Below is the hard way
$characters = "abcdefg"
$charCount = ($characters.ToCharArray() | Where-Object {$_} | Measure-Object).Count

write-host
$charCount

PS C:\scripts\powershell
7

Substituting $ZISVIEW for $characters and putting $charCount in the IF statement shows the correct number of characters and allows me to key off them accordingly, either with no image safe data or a name. Thank you all for your help with this.
0 Likes
Micro Focus Expert
Micro Focus Expert

I'm out of town right now.....
But just so I'm clear....do you have it working for you now? (Even if more complex than desired?





ecmastrean;2485725 wrote:
Thank you Craig and sperrin for your suggestions. When I checked for length <=2 and had it prompt for no image safe data it worked fine when I ran zisedit -c beforehand to clear the data to simulate a new drive, or a computer with no image safe data. So that worked as intended. Yet when I ran the script again with an existing name (and I ran zisview ComputeName to verify the name existed) the script said no name existed and prompted for a name. When I tested this in Powershell ISE and I set the name to a value (12345-LT, or $null or two spaces, the script worked fine. So I was surprised it did not work in the Powershell script running in the WinPE environment. Here is what I changed the script to:

$ZISNAME= zisview ComputerName

If ($ZISNAME.length -le 2)
{
Do
{
Write-Host ""
Write-Host "No Zenworks image-safe data found. A workstation name is required to continue." -ForegroundColor White -BackgroundColor Red
Write-Host ""
$WSNAME = Read-Host -Prompt "Enter the workstation name"
Get-Variable WSNAME -ValueOnly
if($WSNAME)
{
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
$ZISNAME= $WSNAME
Write-Host "The workstation name is $ZISNAME"
Write-Host ""
}

elseif ($WSNAME.length -le 2)
{
$ZISNAME.length -eq 2
}


}
While($ZISNAME.length -le 2)
}

ElseIf ($ZISNAME.length -gt 2)

{
Write-Host ""
Write-Host ""
Write-Host "Computer Name: $ZISNAME"
Write-Host ""
$WSNAME = Read-Host -Prompt "Change Name (Enter Name to change, press Enter to keep existing)"
Get-Variable WSNAME -ValueOnly
if ($WSNAME)
{
Write-Host "Name Changed to $WSNAME"
Write-Host ""
zisedit ScriptedImage=True
zisedit ComputerName=$WSNAME
zisedit dnsHostname=$WSNAME
$ZISNAME= $WSNAME
}
}
--
Please give a hearty thumbs up to any post you find helpful!
To find articles by Craig Wilson simply follow the link: Craig Wilson's Tips!
0 Likes
Ensign
Ensign

Yes everything is working now. It wasn't too much harder, just adapting my code to the other way shown on the site. The site had an "easy way" and a "hard way". Since the easy way didn't work, I tried the other. I'm just glad it's working. Thanks for the suggestions. In case anyone comes across this thread, here is the site I am referring to:

http://brilliantlyeasy.com/powershell-count-character-in-string-variable/
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.