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

时间:2011-05-27 04:03:32

标签: scripting powershell powershell-v2.0

我找到了一个PowerShell script,它可以更改我的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 )
}

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

  

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

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

  

C:\ scripts \ Set-Wallpaper.ps1:无法使用   转换类型的对象   输入'System.Object []'   '的System.Type'。

     

在行:1字符:29

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

但是当我第二次使用完全相同的命令和参数调用脚本时。

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

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

3 个答案:

答案 0 :(得分:7)

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

$wp

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

尝试在没有Add-Type的情况下致电-Passthru,然后分配给$wp

Add-Type -typedef @"
...
"@
$WP = [Wallpaper.Setter]

答案 1 :(得分:5)

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

try {
   $WP = [Wallpaper.Setter]
} catch {
   add-type @"
....
"@
    $WP = [Wallpaper.Setter]
}

你可以通过逐行运行并查看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

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

答案 2 :(得分:0)

Inside Powershell脚本:

PS c:\> & 'C:\scripts\Set-Wallpaper.ps1' C:\Users\myProfile\Pictures\MyWallpaper.jpg

来自cmd.exe

C:\> PowerShell -command "& 'C:\scripts\Set-Wallpaper.ps1' C:\Users\myProfile\Pictures\MyWallpaper.jpg"
相关问题