M
MS-User
Threadstarter
- Dabei seit
- 20.09.2016
- Beiträge
- 94.182
ich habe einen Skriptblock, den ich gerne vor andere Skripte als Funktion setzen möchte.
Das Skript funktioniert an sich, allerdings nicht in einer Funktion. (Weder der erste Teil, der Regkeys setzt, noch der zweite Teil, der ein Desktop Icon setzt. Die beiden Teile sind durch drei Rauten gekennzeichnet.)
Ich vermute, die Funktion komm mit $MyInvocation nicht zurecht?!
Wie kann ich das Skript in einer Funktion aufrufen?
Skript, wie es funktioniert:
### Überprüfen, ob PS Skripte als Admin und als anderer User über das Kontextmenü ausgeführt werden können. Falls nicht, Wert hinzufügen.
$Regkeys = test-path "HKLM:\Software\Classes\Microsoft.PowershellScript.1\Shell\runas"
if ($regkeys -eq $false)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# Füge "Ausführen als..." und "Als Administrator ausführen" für PS Skripte dem Kontextmenü zu
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Als anderer Benutzer ausführen\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" "Start-Process Powershell.exe -Credential (Get-Credential) -ArgumentList %1"'
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit -file "%1"'
}
else
{}
### Verknüpfung auf dem Desktop erstellen, falls nicht vorhanden
# Name des Skripts ohne .ps1
$Scriptname = $MyInvocation.MyCommand.Name
$Scriptname = $Scriptname.replace(".ps1", $null)
# Pfad des Skripts mit Name und Endung
$TargetFile = $MyInvocation.MyCommand.Path
$ShortcutFile = "$env:USERPROFILE\Desktop" + "\$Scriptname.lnk"
# Überprüfen, ob shortcut zu diesem skript auf dem Desktop existiert. Falls nicht, anlegen
$ShortcutExist = test-path $ShortcutFile
if ($ShortcutExist -eq $false)
{
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.WorkingDirectory = "$env:SystemRoot\System32\WindowsPowerShell\v1.0"
$Shortcut.Save()
}
else
{}
Skript als Funktion:
Function Set-DesktopIconAndRegkeys
{
### Überprüfen, ob PS Skripte als Admin und als anderer User über das Kontextmenü ausgeführt werden können. Falls nicht, Wert hinzufügen.
$Regkeys = test-path "HKLM:\Software\Classes\Microsoft.PowershellScript.1\Shell\runas"
if ($regkeys -eq $false)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# Füge "Ausführen als..." und "Als Administrator ausführen" für PS Skripte dem Kontextmenü zu
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Als anderer Benutzer ausführen\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" "Start-Process Powershell.exe -Credential (Get-Credential) -ArgumentList %1"'
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit -file "%1"'
}
else
{}
### Verknüpfung auf dem Desktop erstellen, falls nicht vorhanden
# Name des Skripts ohne .ps1
$Scriptname = $MyInvocation.MyCommand.Name
$Scriptname = $Scriptname.replace(".ps1", $null)
# Pfad des Skripts mit Name und Endung
$TargetFile = $MyInvocation.MyCommand.Path
$ShortcutFile = "$env:USERPROFILE\Desktop" + "\$Scriptname.lnk"
# Überprüfen, ob shortcut zu diesem skript auf dem Desktop existiert. Falls nicht, anlegen
$ShortcutExist = test-path $ShortcutFile
if ($ShortcutExist -eq $false)
{
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.WorkingDirectory = "$env:SystemRoot\System32\WindowsPowerShell\v1.0"
$Shortcut.Save()
}
else
{}
}
Set-DesktopIconAndRegkeys
Das Skript funktioniert an sich, allerdings nicht in einer Funktion. (Weder der erste Teil, der Regkeys setzt, noch der zweite Teil, der ein Desktop Icon setzt. Die beiden Teile sind durch drei Rauten gekennzeichnet.)
Ich vermute, die Funktion komm mit $MyInvocation nicht zurecht?!
Wie kann ich das Skript in einer Funktion aufrufen?
Skript, wie es funktioniert:
### Überprüfen, ob PS Skripte als Admin und als anderer User über das Kontextmenü ausgeführt werden können. Falls nicht, Wert hinzufügen.
$Regkeys = test-path "HKLM:\Software\Classes\Microsoft.PowershellScript.1\Shell\runas"
if ($regkeys -eq $false)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# Füge "Ausführen als..." und "Als Administrator ausführen" für PS Skripte dem Kontextmenü zu
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Als anderer Benutzer ausführen\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" "Start-Process Powershell.exe -Credential (Get-Credential) -ArgumentList %1"'
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit -file "%1"'
}
else
{}
### Verknüpfung auf dem Desktop erstellen, falls nicht vorhanden
# Name des Skripts ohne .ps1
$Scriptname = $MyInvocation.MyCommand.Name
$Scriptname = $Scriptname.replace(".ps1", $null)
# Pfad des Skripts mit Name und Endung
$TargetFile = $MyInvocation.MyCommand.Path
$ShortcutFile = "$env:USERPROFILE\Desktop" + "\$Scriptname.lnk"
# Überprüfen, ob shortcut zu diesem skript auf dem Desktop existiert. Falls nicht, anlegen
$ShortcutExist = test-path $ShortcutFile
if ($ShortcutExist -eq $false)
{
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.WorkingDirectory = "$env:SystemRoot\System32\WindowsPowerShell\v1.0"
$Shortcut.Save()
}
else
{}
Skript als Funktion:
Function Set-DesktopIconAndRegkeys
{
### Überprüfen, ob PS Skripte als Admin und als anderer User über das Kontextmenü ausgeführt werden können. Falls nicht, Wert hinzufügen.
$Regkeys = test-path "HKLM:\Software\Classes\Microsoft.PowershellScript.1\Shell\runas"
if ($regkeys -eq $false)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# Füge "Ausführen als..." und "Als Administrator ausführen" für PS Skripte dem Kontextmenü zu
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Als anderer Benutzer ausführen\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" "Start-Process Powershell.exe -Credential (Get-Credential) -ArgumentList %1"'
New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" `
-Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit -file "%1"'
}
else
{}
### Verknüpfung auf dem Desktop erstellen, falls nicht vorhanden
# Name des Skripts ohne .ps1
$Scriptname = $MyInvocation.MyCommand.Name
$Scriptname = $Scriptname.replace(".ps1", $null)
# Pfad des Skripts mit Name und Endung
$TargetFile = $MyInvocation.MyCommand.Path
$ShortcutFile = "$env:USERPROFILE\Desktop" + "\$Scriptname.lnk"
# Überprüfen, ob shortcut zu diesem skript auf dem Desktop existiert. Falls nicht, anlegen
$ShortcutExist = test-path $ShortcutFile
if ($ShortcutExist -eq $false)
{
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.WorkingDirectory = "$env:SystemRoot\System32\WindowsPowerShell\v1.0"
$Shortcut.Save()
}
else
{}
}
Set-DesktopIconAndRegkeys