从C#调用时,工作的Powershell函数无法完成工作

时间:2019-01-10 13:50:36

标签: c# powershell

好像我解释了我的问题还不够好。所以这里是我的编辑:

我有一个Powershell功能,用于取消固定Windows开始菜单图块。 从此开始:imgur.com/a/0hRhDkW 为此:imgur.com/a/rMCkfR9

该功能完成了她应该做的工作,并且没有任何错误。 我可以像这样通过bat调用* .ps1文件:


    c:\Windows\System32\windowspowershell\v1.0\powershell.exe -NoProfile -executionpolicy remotesigned -File "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\self-deleting-script_user-settings.ps1"

或仅通过Powershell ISE启动它。

这就是我们正在讨论的powershell函数:


    function UnpinTiles
    {
        (New-Object -Com Shell.Application).
            NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').
            Items() | %{ $_.Verbs() } | ?{$_.Name -match 'Un.*pin from Start'} | %{$_.DoIt()}
    }

该功能是用户配置脚本的一部分。 也许有人对整个事情感兴趣。在这里,您去了:


    #Info#
    #   administative privileges are needed, to write to locations that are shared by multiple users.
    #   therfore, you can only write to HKEY_CURRENT_USER and that's all we need!
    #Info#



    ## load into memory, Self deleting
    Remove-Item $MyINvocation.InvocationName


    ## Window Design
    [console]::ForegroundColor = "Blue"
    [console]::BackgroundColor = "White"
    #title
    $host.ui.RawUI.WindowTitle = "Flex - Configure User"
    cls

    #-------------------------------------------------------------#



    ## unpin all start menu tiles (english only. maybe change ?{$_.Name -match 'Un.*pin from Start'} for different language)
    function UnpinTiles
    {
        (New-Object -Com Shell.Application).
            NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').
            Items() | %{ $_.Verbs() } | ?{$_.Name -match 'Un.*pin from Start'} | %{$_.DoIt()}
    }


    #----------------------RegTweaks-START----------------------#
    ##________________________________________________________________________Format
    #Write-Host "   what i am doing" -ForegroundColor Gray
    #If (!(Test-Path "HKCU:\")) {
    #    New-Item -Path "HKCU:\" >$null 2>&1
    #}
    #Set-ItemProperty -Path "HKCU:\" -Name "" -Type DWord -Value 0 >$null 2>&1
    ##________________________________________________________________________Format


    try 
    {

    #Error Silent
    $ErrorActionPreference = 'SilentlyContinue'
        function RegTweaks
        {
            #Disable Windows Hello Notifications
            Write-Host "    Disable Windows Hello Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.HelloFace")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.HelloFace" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.HelloFace" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable News Notifications
            Write-Host "    Disable News Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.BingNews_8wekyb3d8bbwe!AppexNews")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.BingNews_8wekyb3d8bbwe!AppexNews" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.BingNews_8wekyb3d8bbwe!AppexNews" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable Edge Notifications
            Write-Host "    Disable Edge Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable Suggested Notifications
            Write-Host "    Disable Suggested Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.Suggested")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.Suggested" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.Suggested" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable Store Notifications
            Write-Host "    Disable Store Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.WindowsStore_8wekyb3d8bbwe!App")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.WindowsStore_8wekyb3d8bbwe!App" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Microsoft.WindowsStore_8wekyb3d8bbwe!App" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable Security and Maintenance Notifications
            Write-Host "    Disable Security and Maintenance Notifications" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.SecurityAndMaintenance")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.SecurityAndMaintenance" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Windows.SystemToast.SecurityAndMaintenance" -Name "Enabled" -Type DWord -Value 0 >$null 2>&1

            #Disable Hotkey to Toggle Color Filters On or Off in Windows 10
            Write-Host "    Disable Hotkey to Toggle Color Filters On or Off in Windows 10" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\ColorFiltering")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\ColorFiltering" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\ColorFiltering" -Name "HotkeyEnabled" -Type DWord -Value 0 >$null 2>&1

            #Add This PC Desktop Icon
            Write-Host "    Add This PC Desktop Icon" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel" -Name "{20D04FE0-3AEA-1069-A2D8-08002B30309D}" -Type DWord -Value 0 >$null 2>&1

            #Taskbar, Hide Task View Button
            Write-Host "    Taskbar, Hide Task View Button" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ShowTaskViewButton" -Type DWord -Value 0 >$null 2>&1

            #Taskbar, Hide Search
            Write-Host "    Taskbar, Hide Search" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search" -Name "SearchboxTaskbarMode" -Type DWord -Value 0 >$null 2>&1

            #Show File Name Extensions
            Write-Host "    Show File Name Extensions" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "HideFileExt" -Type DWord -Value 0 >$null 2>&1

            #Prevent Sticky Keys
            Write-Host "    Prevent Sticky Keys" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\Control Panel\Accessibility\StickyKeys")) {
                New-Item -Path "HKCU:\Control Panel\Accessibility\StickyKeys" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\Control Panel\Accessibility\StickyKeys" -Name "Flags" -Type String -Value 506 >$null 2>&1

            #Make Desktop Menus Appear Faster
            Write-Host "    Prevent Sticky Keys" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\Control Panel\Desktop")) {
                New-Item -Path "HKCU:\Control Panel\Desktop" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "MenuShowDelay" -Type String -Value 200 >$null 2>&1

            #Taskbar, Hide People Button
            Write-Host "    Prevent Sticky Keys" -ForegroundColor Gray
            If (!(Test-Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\People")) {
                New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\People" >$null 2>&1
            }
            Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\People" -Name "PeopleBand" -Type String -Value 0 >$null 2>&1
        }    
    }
    catch 
    {}
    #------------------------RegTeaks-END-----------------------#








    #==========================================
    # EXECUTION 
    #==========================================
    cls
    $EXcount = 1
    Write-Host "        ========================================" -ForegroundColor Black
    Write-Host "        =========== Configuring User ===========" -ForegroundColor Black
    Write-Host -NoNewline "        ====== " -ForegroundColor Black
    Write-Host -NoNewline "Do not close this window!!" -ForegroundColor Red
    Write-Host " ======" -ForegroundColor Black
    Write-Host "        ========================================" -ForegroundColor Black
    Write-Host "`n"

    ## unpin all start menu tiles
    Write-Host "`n$EXcount. Unpin StartMenu tiles."
    $EXcount++
    UnpinTiles
    Write-Host -NoNewline "______________________________________________________|"
    Write-Host "Done!" -ForegroundColor Green


    ##Registry tweaks
    Write-Host "`n$EXcount. Adding Registry Tweaks"
    $EXcount++
    RegTweaks
    Write-Host -NoNewline "______________________________________________________|"
    Write-Host "Done!" -ForegroundColor Green

现在是我的问题。 据说一切正常,只是当我尝试从这样的C#程序中调用* .ps1时:

    Process.Start("C:\\Windows\\System32\\windowspowershell\\v1.0\\powershell.exe", $"-ExecutionPolicy ByPass -File {PathToFile}"); 

这些功能可以运行,但是Windows的开始菜单图块仍然存在。 而且我必须知道为什么他不能这样做。

所以

    我通过蝙蝠称呼它,它可以删除Windows的开始菜单图块。
  • 我通过C#调用它,无法删除Windows星型菜单图块。

1 个答案:

答案 0 :(得分:0)

如果这样...

PathToFile

…是具有此功能的脚本的完整UNC,然后如TheIncorrigible1所述,您只是调用脚本,而不是其中的函数,直到更改发布到此的内容。

function UnpinTiles
{
    (New-Object -Com Shell.Application).
        NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').
        Items() | %{ $_.Verbs() } | ?{$_.Name -match 'Un.*pin from Start'} | %{$_.DoIt()}
}

# Call the function
UnpinTiles

如果您只想在调用脚本时运行该块,则只需执行此操作。

(New-Object -Com Shell.Application).
NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').
Items() | 
%{ $_.Verbs() } | 
?{$_.Name -match 'Un.*pin from Start'} | 
%{$_.DoIt()}

如果您说的是脚本中有多个函数,那么您可以运行文件来加载脚本,但仍然必须按照TheIncorrigible1的建议调用该函数。