[powershell] registerkey-functie aanvaard variabelen niet

Pagina: 1
Acties:

Vraag


  • RainerSGF
  • Registratie: Juli 2008
  • Laatst online: 26-11 23:41
Hallo,
ik heb een simpele powershell-functie aangemaakt als volgt:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$global:rtgpath = "HKLM:\SOFTWARE"
....
function Set_RegistryKey($regkeyname, $regpath, $type, $regvalue)
{
  if(!(Test-Path $regpath))
  {
    New-Item -Path $regpath -Force | Out-Null
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
  else
  {
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
}


Wanneer ik echter dan later deze functie aanroep als volgt:

code:
1
  Set_RegistryKey("abc", $global:rtgpath, "string", "def")


Dan krijg ik de foutmelding Cannot validate argument on parameter 'Path' because it is null. .... De variabele $regpath zou dus null zijn.

Ik begrijp er niets meer van.

Systeem - I'm learning, I learn it from a book :)

Beste antwoord (via RainerSGF op 05-06-2017 18:15)


  • Squ1zZy
  • Registratie: April 2011
  • Niet online
RainerSGF schreef op maandag 5 juni 2017 @ 16:19:
Hallo,
ik heb een simpele powershell-functie aangemaakt als volgt:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$global:rtgpath = "HKLM:\SOFTWARE"
....
function Set_RegistryKey($regkeyname, $regpath, $type, $regvalue)
{
  if(!(Test-Path $regpath))
  {
    New-Item -Path $regpath -Force | Out-Null
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
  else
  {
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
}


Wanneer ik echter dan later deze functie aanroep als volgt:

code:
1
  Set_RegistryKey("abc", $global:rtgpath, "string", "def")


Dan krijg ik de foutmelding Cannot validate argument on parameter 'Path' because it is null. .... De variabele $regpath zou dus null zijn.

Ik begrijp er niets meer van.
De comma's moeten weg bij het aanroepen van de functie. Jij maakt er een array van :)

code:
1
Set_RegistryKey "abc" $global:rtgpath "string" "def"


Global mag ook weg :)

Alle reacties


Acties:
  • Beste antwoord

  • Squ1zZy
  • Registratie: April 2011
  • Niet online
RainerSGF schreef op maandag 5 juni 2017 @ 16:19:
Hallo,
ik heb een simpele powershell-functie aangemaakt als volgt:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$global:rtgpath = "HKLM:\SOFTWARE"
....
function Set_RegistryKey($regkeyname, $regpath, $type, $regvalue)
{
  if(!(Test-Path $regpath))
  {
    New-Item -Path $regpath -Force | Out-Null
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
  else
  {
    New-ItemProperty -Path $regpath -Name $regkeyname -PropertyType $type -Value $regvalue | Out-null
  }
}


Wanneer ik echter dan later deze functie aanroep als volgt:

code:
1
  Set_RegistryKey("abc", $global:rtgpath, "string", "def")


Dan krijg ik de foutmelding Cannot validate argument on parameter 'Path' because it is null. .... De variabele $regpath zou dus null zijn.

Ik begrijp er niets meer van.
De comma's moeten weg bij het aanroepen van de functie. Jij maakt er een array van :)

code:
1
Set_RegistryKey "abc" $global:rtgpath "string" "def"


Global mag ook weg :)

  • RainerSGF
  • Registratie: Juli 2008
  • Laatst online: 26-11 23:41
dank u wel!

Systeem - I'm learning, I learn it from a book :)


  • Squ1zZy
  • Registratie: April 2011
  • Niet online
You're welcome!
Mooie community heeft Tweakers ... Elkaar helpen en steunen als we dat kunnen _/-\o_