Link to home
Start Free TrialLog in
Avatar of John Davies
John DaviesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Variables and Strings problem

Newby here, sorry.

I am trying to write a script that will take the machine name and store and amend variable and use that throughout.

LINE 1 - $PC will get the hostname of the machine the script is run on
LINE 2 - Trim the machine name by 6 chars from right, currently we have A16ROOM-12345 as an example, WHEN COMPLETE IT WOULD BE A16ROOM
LINE 4 - Value DOMAIN\$PC I would like to use the DOMAIN which will be static and the shortened machine name from LINE 2
LINE 5 - LOCATION SHORTCODEpassword - Location Shortcode will be the $PC hostname but just using the first Characters up to but not including R followed by some characters that we would add in the code manually, as an exanple A16PASSWORD

Hope this makes sense, as I am new to cmd ia m finding this bit a little difficult.

Would anyone be able to provide the code for those few lines.

Thanks in Advance

John


1/ $PC = (Get-WmiObject Win32_ComputerSystem).Name "create a variable based on machine name"
2/ $PC = $PC.Substring(myString.Length-5) "trim that variable (pc name) by 5 characters from the right"


# Here we are changing the values of the registry keys with the "Set-ItemProperty" syntax. -Name denotes the actual key that you are changing
3/ Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 1
4/ Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value DOMAIN\$PC
5/ Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value LOCATION SHORTCODEpassword
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Like this:


$PC = (Get-WmiObject Win32_ComputerSystem).Name 
$PC = "A16ROOM-12345" 
 
$PC = $PC.Substring(1,$PC.Length-7) 
$ADPC = "ADOMAIN\$PC" 
Write-Host $ADPC 
 
$OK = $PC -match '(\w\d+)' 
$PW = "DEFAULTPW" 
If ($OK) {$PW = $matches[1] + "PASSWORD"} 
Write-Host $PW

Open in new window



Avatar of John Davies

ASKER

Thanks Zvonko for your input, how would I use the information (you provided) to add to these registry values I have put below, obviously it the information in the value I am trying to edit.


Sorry, new to these.


thanks


John


Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value DOMAIN\$PC
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value LOCATION SHORTCODEpassword

Did you test my code? Does it yield what you have expected?


ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

I have run what you put in cmd but get a few errors, not a problem. But what i am trying to do is to manipulate the values going into the registry based on the variables and trimming them as per the code and add them to the lines of code below.


Thanks for your patience, what I am after is how would I add what you stated into the code below where I have bolded and commented


#Here are a list of your variables each variable is needed as it determines which method within the script to run. 

$PC = (Get-WmiObject Win32_ComputerSystem).Name "create a variable based on machine name" 
$PC = $PC.Substring(myString.Length-7) "trim that variable ($PC name) by 7 characters from the right" for example Hostname is A16ROOM-123456 would become A16ROOM
 
 
# Here we are changing the values of the registry keys with the "Set-ItemProperty" syntax. -Name denotes the actual key that you are changing 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 1 

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value DOMAIN\$PC ($PC trimmed from 7 characters from the right), so A16ROOM

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value LOCATION SHORTCODEpassword - (Location shortcode is the first characters up to and excluding the letter R so for example 'A16Room' would become 'A16' and then add what we put as the password in the value, for example 'a16password'

New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Name DisableLockWorkstation -Value 1 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name HideFastUserSwitching -Value 1 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1 
#Test-Path enables the us to test to see whether the path is either true or false (Exists or doesn't) if the paths boolean is false then it will create a new item. 
Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon" 
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon") 
{ 
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1 
} 
#If the the correct syntax is detected the user will be displayed a window notifying them that the reg edit has completed. 
$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("You have applied the registry edits to $PC",0,"Complete!",0) 
} 
else 
{ 
$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("Sorry your computer does not have the correct naming convention for use with this Script, contact Josh at IT for assistance",0,"Warning!",0)

Open in new window

Amazing Zvonko, really appreciate your help with this, was thinking to much about it. STAR!!!!

Glad it worked for you.


Check please against this version:

#Here are a list of your variables each variable is needed as it determines which method within the script to run.  
 
$PC = (Get-WmiObject Win32_ComputerSystem).Name #"create a variable based on machine name"  
$PC = $PC.Substring(0,$PC.Length-6) "trim that variable ($PC name) by 7 characters from the right" for example Hostname is A16ROOM-123456 would become A16ROOM 
$ADPC = "yourdomainhere\$PC" 	# CHANGE yourdomainhere 
 
$OK = $PC -match '(\w+\d+)' 
$PW = "DEFAULTPW" 				# CHANGE DEFAULTPW  if $PC is not Characters followed by Digits 
If ($OK) {$PW = $matches[1] + "PASSWORD"}  
  
# Here we are changing the values of the registry keys with the "Set-ItemProperty" syntax. -Name denotes the actual key that you are changing  
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 1  
 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value $ADPC #DOMAIN\$PC ($PC trimmed from 7 characters from the right), so A16ROOM 
 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $PW 	#LOCATION SHORTCODEpassword - (Location shortcode is the first characters up to and excluding the letter R so for example 'A16Room' would become 'A16' and then add what we put as the password in the value, for example 'a16password' 
 
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Name DisableLockWorkstation -Value 1  
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name HideFastUserSwitching -Value 1  
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1  
#Test-Path enables the us to test to see whether the path is either true or false (Exists or doesn't) if the paths boolean is false then it will create a new item.  
Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon"  
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")  
{  
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1  
}  
#If the the correct syntax is detected the user will be displayed a window notifying them that the reg edit has completed.  
$wshell = New-Object -ComObject Wscript.Shell  
$wshell.Popup("You have applied the registry edits to $PC",0,"Complete!",0)  
}  
else  
{  
$wshell = New-Object -ComObject Wscript.Shell  
$wshell.Popup("Sorry your computer does not have the correct naming convention for use with this Script, contact Josh at IT for assistance",0,"Warning!",0)

Open in new window

Sorry Zvonko,


In my haste I noticed something


$PC = (Get-WmiObject Win32_ComputerSystem).Name
$PC = "A16ROOM-12345"


Everytime I run the script it uses 'A16ROOM' rather than the HOSTNAME minus 7 chars, some machines are A16Room-123456, some are A16Level-123456 or A16Floor-123456


When i run the above I just get A16ROOM


Thanks


John

Sory, my fault.


Simply remove this hardcoded line for MY tests:


$PC = "A16ROOM-12345"


Therefore you have not the real PC name.


And please compare the lines from my last big Code Snippet.




Sorry Zvonko

Have run again, great but one more issue


The password is made up of the all the characters leading up the R, L, F so for example

A16Floor

A100Room

A7Floor

would be 


A16

A100

A7


and then I would add a default password at the end, so if I use 'PASSWORD' as the default password they would become


A16PASSWORD

A100PASSWORD

A7PASSWORD


This bit of code just displays DEFAULTPW when run


$OK = $PC -match '(\w\d+)' $PW = "DEFAULTPW" If ($OK) {$PW = $matches[1] + "PASSWORD"} Write-Host $PW

Are that four lines of code in your script?

$OK = $PC -match '(\w\d+)' 
$PW = "DEFAULTPW" 
If ($OK) {$PW = $matches[1] + "PASSWORD"} 
Write-Host $PW  

Open in new window

Add one + character in the mask just in case you have more then one character at name starting:

$OK = $PC -match '(\w+\d+)'  
$PW = "DEFAULTPW"  
If ($OK) {$PW = $matches[1] + "PASSWORD"}  
Write-Host $PW

Open in new window

Hi Yes, they are, I have attached the script


As an admin, running the script and checking the values of the two registry keys, neither changed from the initial build


Thanks


Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value $ADPC 

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $PW

Open in new window


All code I am using


################################################################################################## 
# A powershell script that can be used to manipulate the registry keys for a ward specific logon # 
################################################################################################## 
 
#Here are a list of your variables each variable is needed as it determines which method within the script to run. 
$PC = (Get-WmiObject Win32_ComputerSystem).Name 
 
$PC = $PC.Substring(0,$PC.Length-6) 
$ADPC = "WSH\$PC" 
Write-Host $ADPC 
 
$OK = $PC -match '(\w\d+)' 
$PW = "DEFAULTPW" 
If ($OK) {$PW = $matches[1] + "password"} 
Write-Host $PW 
 
 
# Here we are changing the values of the registry keys with the "Set-ItemProperty" syntax. -Name denotes the actual key that you are changing 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 1 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value $ADPC 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $PW 
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Name DisableLockWorkstation -Value 1 
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name HideFastUserSwitching -Value 1 
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1 
#Test-Path enables the us to test to see whether the path is either true or false (Exists or doesn't) if the paths boolean is false then it will create a new item. 
Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon" 
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon") 
{ 
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1 
} 
#If the the correct syntax is detected the user will be displayed a window notifying them that the reg edit has completed. 
$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("You have applied the registry edits to $PC",0,"Complete!",0) 
}

Open in new window

Admins make also faults  :  )


Tell'm they need to run it in an Administrative DOS prompt.

It is not enought to have Administrator account. 

The command window needs also rised provileges.



Hi Zvonko, Seems like there is an issue from our side with the naming convention, quick question,


How do i concatonate strings and variables, for example


$ADPC = "WSH\$PC" you put this and would produce the machine name, e.g adcd-123456, then trimmed would produce adcd

what If I wanted to put room on the front and w10 on the back with no space, e.g ROOMADCDW10, trying everything here and can get the front ROOMADCD but not the W10


Thanks Again


John

Hello John,


I have not understood your upper example.


Let's start like this:

$PC = $PC.Substring(0,$PC.Length-6)
$ADPC = "WSH\" + $PC


The last line creates a varible for Active Directory + PC name.


Give me some example for the change.

What $PC name and what $ADPC result you want to have.



Hi Zvonko,


Thanks for getting back, sorry for my late reply, meeting after meeting yesterday afternoon.


$PC = $PC.Substring(0,$PC.Length-6)
$ADPC = "WSH\" + $PC


I see this does what is says, trims from the right, what I would like is, is there a way of prefixing and adding before and after the TRIMMED machine name, change in naming convention


For Example

Original Machine Name: A16ROOM-123456 becomes A16ROOM

With the A16ROOM, can it be strings be added before and after? So as an example SOUTHA16ROOMW10 (This would be the new machine name)


Thanks


John


I would preffer to open a new Question for more details.


Here is a new version for combining computer name with prefix and suffix strings.

But for sure you have more exceptions where this script will not work.


Therefore open please a new question and post perhaps this script as starting point.


$ADDOM = "WSH" 
$PCPRE = "SOUTH" 
$PCSUF = "W10" 
$PC = (Get-WmiObject Win32_ComputerSystem).Name  
$PC = "A16ROOM.123456" 
$PART = $PC -split '-' 
If ($PART[1]) { 
	$PC = $PCPRE+$PART[0]+$PCSUF 
} else { 
	$MATCH = $PC -match '^(\w+\d+\w+)' 
	If ($MATCH) { 
		$PC = $PCPRE+$matches[0]+$PCSUF 
	} else { 
		$PC = $PCPRE+$PC.Substring(0,$PC.Length-6)+$PCSUF 
	} 
} 
$ADPC = $ADDOM+"\"+$PC  
Write-Host $ADPC 

Open in new window