PowerShell脚本第一次失败但第二次失败

Spa*_*pan 5 powershell scripting powershell-2.0

我找到了一个PowerShell脚本,可以更改我的Windows 7 PC桌面壁纸的图像文件,其路径作为参数提供.我想要的最终结果是在启动时通过批处理文件调用此脚本.

[CmdletBinding()]
Param(
   [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
   [Alias("FullName")]
   [string]
   $Path
,
   [Parameter(Position=1, Mandatory=$false)]
   $Style = "NoChange"
)

BEGIN {
try {
   $WP = [Wallpaper.Setter]
} catch {
   $WP = add-type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
     Tile, Center, Stretch, NoChange
}

public class Setter {
  public const int SetDesktopWallpaper = 20;
  public const int UpdateIniFile = 0x01;
  public const int SendWinIniChange = 0x02;

  [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);

  public static void SetWallpaper ( string path, Wallpaper.Style style ) {
     SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );

     RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
     switch( style )
     {
        case Style.Stretch :
           key.SetValue(@"WallpaperStyle", "2") ; 
           key.SetValue(@"TileWallpaper", "0") ;
           break;
        case Style.Center :
           key.SetValue(@"WallpaperStyle", "1") ; 
           key.SetValue(@"TileWallpaper", "0") ; 
           break;
        case Style.Tile :
           key.SetValue(@"WallpaperStyle", "1") ; 
           key.SetValue(@"TileWallpaper", "1") ;
           break;
        case Style.NoChange :
           break;
     }
     key.Close();
    }
   }
}
"@ -Passthru
}
}
PROCESS {
   Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
   $WP::SetWallpaper( (Convert-Path $Path), $Style )
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下命令调用此脚本:

C:\ scripts\Set-Wallpaper.ps1 C:\ Users\myProfile\Pictures\MyWallpaper.jpg

我对PowerShell脚本的世界完全不熟悉,而我遇到的问题是,当我从PowerShell中执行脚本时,它总是在第一次失败时出现以下错误:

C:\ scripts\Set-Wallpaper.ps1:无法将类型为'System.Object []'的对象强制转换为'System.Type'.

在行:1 char:29

  • C:\ scripts\Set-Wallpaper.ps1 <<<< C:\ Users\mbaleato\Pictures\MyWallpaper.jpg
    • CategoryInfo:NotSpecified:(:) [Set-Wallpaper.ps1],InvalidCastException
    • FullyQualifiedErrorId:System.InvalidCastException,Set-Wallpaper.ps1

但是当我第二次使用完全相同的命令和参数调用脚本时,它的工作原理.

这是第一次导致我的批处理文件失败时失败.

任何经验丰富的人都会对第一次失败的原因提出一些建议,但第二次有效吗?关于如何让它第一次运作的任何建议?

ste*_*tej 7

看看以开头的行$WP = add-type @".那就是问题所在.您创建两种类型:

$wp

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Style                                    System.Enum
True     False    Setter                                   System.Object
Run Code Online (Sandbox Code Playgroud)

尝试在Add-Type没有-Passthru和之后拨打电话$wp

Add-Type -typedef @"
...
"@
$WP = [Wallpaper.Setter]
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 5

我相信这是因为 - passthru正在将$ WP变成一个数组 - 你可以尝试这个试试这个:

try {
   $WP = [Wallpaper.Setter]
} catch {
   add-type @"
....
"@
    $WP = [Wallpaper.Setter]
}
Run Code Online (Sandbox Code Playgroud)

您可以通过逐行运行并检查tyoe来查看:

PS D:\bin\OpenSSL-Win32\bin> $WP

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Style                                    System.Enum
True     False    Setter                                   System.Object

PS D:\bin\OpenSSL-Win32\bin> $WP.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


PS D:\bin\OpenSSL-Win32\bin> $WP = [Wallpaper.Setter]
PS D:\bin\OpenSSL-Win32\bin> $WP.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    True     RuntimeType                              System.Type
Run Code Online (Sandbox Code Playgroud)

第二次类型已经存在,所以%WP正确加载.