Skript

Diskutiere Skript im Windows 10 Forum im Bereich Microsoft Community; ich habe einen Skriptblock, den ich gerne vor andere Skripte als Funktion setzen möchte. Das Skript funktioniert an sich, allerdings nicht in...
  • Skript Beitrag #1
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
 
Thema:

Skript

Skript - Ähnliche Themen

GELÖST powershell zuordnen: Hallo, hier ein Pro mit version 1909. möchte gerne eine Datei mit Endung ps1 der Powershell zuordnen. Leider steht dies nicht zur Auswahl und...
Browser liefert Seiten aber curl nicht: Hallo Leute, Ich habe ein Tool programmiert das mir von Ebay Kleinanzeigen für einige Orte auf einer Route die Ergebnisse im Browser öffnet. Nun...
Windows Store Broken: If you search for Windows Store broken you find A LOT of Questions and (useless) answers, so no answer helped I am looking for support here...
windows explorer crash when opening a file: My windows explorer stops working repetitively after last windows update. When I open the explorer and browse some folders it starts opening lots...
Powershell - GetWinEvent Extended Datafield: FailureReason Text ausgabe anstelle des Statuscodes: Hallo, ich versuche, das Event 4625 () mit Powershell auszuwerten. Um an die Datenstruktur zu kommen, nutze ich folgenden Code...
Oben