Porffor
asked on
Gotten a bit confused with my rather simple PowerShell script
With my very limited knowledge of Powershell and some reasearch, I've put together a powershell script that checks if a specific registry value HKCU is true, then if it is, appends a line to a text file on a network share.
This worked, I get a tab-delimited text file with Username, Computername, and the word 'Windows' in separate columns. But then I wanted to adapt the script so that it first checks if the appended line exists in the file - if so, does nothing, if not, appends the line as per the original script. Here is where my Powershell knowledge breaks down unfortunately, I stole bits from other scripts that I found on the net, and I've probably come up with something that doesn't make any sense. It doesn't work anyway, the line doesn't get written at all now.
Could you have a look at the above and explain why it doesn't work? Could you get rid of any lines that I don't need, and also structure it better than I've done, i.e. lines in the correct order and tabbing the lines to the appropriate places?
Many thanks.
$key = 'HKCU:\Control Panel\Desktop'
$PreferredUILanguages = (Get-ItemProperty -Path $key -Name PreferredUILanguages).PreferredUILanguages
if ($PreferredUILanguages -eq "cy-GB")
{
Add-Content "\\server\share\IT\Iaith\Allbwn\Cymraeg.txt" "`n$env:USERNAME`t$env:COMPUTERNAME`tWindows"
}
This worked, I get a tab-delimited text file with Username, Computername, and the word 'Windows' in separate columns. But then I wanted to adapt the script so that it first checks if the appended line exists in the file - if so, does nothing, if not, appends the line as per the original script. Here is where my Powershell knowledge breaks down unfortunately, I stole bits from other scripts that I found on the net, and I've probably come up with something that doesn't make any sense. It doesn't work anyway, the line doesn't get written at all now.
$key = 'HKCU:\Control Panel\Desktop'
$PreferredUILanguages = (Get-ItemProperty -Path $key -Name PreferredUILanguages).PreferredUILanguages
$Path = "\\server\share\IT\Iaith\Allbwn\Cymraeg.txt"
$Value = "$env:USERNAME`t$env:COMPUTERNAME`tWindows"
function Add-FirstLine {
param
(
[string]$Path,
[string]$Value
)
$CurrentContent = Get-Content $Path
Set-Content -Path $Path -Value $Value
Add-Content -Path $Path -Value $CurrentContent
}
if ($PreferredUILanguages -eq "cy-GB")
{
if(!(Get-Content -Path $Path).Contains($Value))
{}
else
{
Add-FirstLine -Path $Path -Value $Value
}
}
Could you have a look at the above and explain why it doesn't work? Could you get rid of any lines that I don't need, and also structure it better than I've done, i.e. lines in the correct order and tabbing the lines to the appropriate places?
Many thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oBda - Another benefit of doing it your way is that it doesn't fail when the text file is empty. In the script that I posted above, if the text file was empty, then I'd get the error...
Open in new window
Which wasn't a massive deal, because it required was for me to manually put something in the file. But your adaptation overcomes that so that's good.
Thanks.