Author Posts

January 1, 2012 at 12:00 am

by Symbiot at 2012-12-05 02:53:53

Hi

What's wrong here:

$ini = "mrt"
$user = Get-ADUser -LDAPFilter "(initials=$ini)"
If ($User -eq $Null)
{"gå videre med runbook eller hvad nu end"}
Else {
New-ItemProperty -Path HKLM:\SOFTWARE\FLAG -Name UserExists -Value 1 -Force
}

it gives me this output:

PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\FLAG
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName : FLAG
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
UserExists : 1

But when I check the registry nothing is there..

by RichardSiddaway at 2012-12-05 04:18:44

How are you checking the registry?

by Symbiot at 2012-12-05 05:27:13

I'm not checking the registry at all.. I just check if user exists.. then want to set a flag in registry..
I wouldn't need to check anything in registry to add a value to reg... would I?

by Klaas at 2012-12-05 07:08:51

I think Richard wants to know how you checked the registry after your script was executed. Just to be sure that you're looking at an updated status. If you navigate through the registry provider it may give you outdated information and you might find that after a refresh the new flag is there after all.

by Symbiot at 2012-12-06 00:31:40

thanks.. I've been doing a manual check, and the refresh doesn't work.. well it does, of course, but I don't get a new key in the registry..

by Klaas at 2012-12-06 04:32:11

I'm sorry, but it's still not 100% clear to me.
A manual check, would that be with a Powershell command or in regedit?
And to avoid misunderstandings regarding terminology: you know the difference between a key and a value, right?
New-ItemProperty creates a value, which is a property of a key, so if you're looking for a new key, there won't be one. Did you check for new values as well?
Maybe I'm splitting hairs, but I want to be really sure we' re talking about the same thing.

by Symbiot at 2012-12-10 02:14:44

hi klaas...

here's the complete code:

$s = New-PSSession ad1 -Authentication Kerberos
Enter-PSSession -Session $s
#Invoke-Command -Session $s -ScriptBlock {Set-Location C:\}
Invoke-Command -session $s -scriptblock { Import-Module ActiveDirectory }
Start-Sleep -Seconds 8

$ini = "mrt"

$user = Get-ADUser -LDAPFilter "(initials=$ini)"
$user
if ($user -eq $Null)
{"Error"}
Else {

New-ItemProperty -Path hklm:\Software\FLAG -Name userExists -Value 2 -Force
}

Get-ItemProperty -Path hklm:\Software\FLAG
exit
$s | Remove-PSSession

The key exits already (key being FLAG) and I check manually by refreshing the registry on my AD server...

by Klaas at 2012-12-17 05:03:17

I think Enter-PSSession does not work in a script. The output you're seeing is the key value you created on your own computer.

by nohandle at 2012-12-20 01:11:27

Try passing the whole script in one scriptblock:
$s = New-PSSession ad1 -Authentication Kerberos
Invoke-Command -session $s -scriptblock {
Import-Module ActiveDirectory
#there has to be better way to check if the module is
#already imported than waiting eight seconds don't you think?
#I'd try looping and get-module
Start-Sleep -Seconds 8
$ini = "mrt"
$user = Get-ADUser -LDAPFilter "(initials=$ini)"
$user
if ($user -eq $Null)
{
write-error "Error"
}
Else
{
New-ItemProperty -Path hklm:\Software\FLAG -Name userExists -Value 2 -Force
}

Get-ItemProperty -Path hklm:\Software\FLAG
}

$s | Remove-PSSession