Question 2 executions du script avant que ça marche

Plus d'informations
il y a 14 ans 9 mois #9709 par Seb
Bonjour à tous,

Débutant en Powershell j'ai fais un petit code qui me permet de changer mon fond d'écran.

Mon soucie est que je suis obligé d'exécuter 2 fois le script pour qu'il fonctionne.

Le voici:

[code:1]BEGIN {
[string]$path= \"C:\orange.bmp\"
[string]$Style= \"NoChange\"
[int]$a = 1
[int]$global:b = 0

function FuncCheckService{

param($ServiceName)

$arrService = Get-Service -Name $ServiceName

if ($arrService.Status -eq \"Running\"«»){
echo $ServiceName \"|-> service ok\"
}else{
$a= $a +1
if($a -eq 30){
$global:b = 1
}else{
echo $ServiceName \"|-> NOK\"
Start-Sleep -m 1000
FuncCheckService -ServiceName $ServiceName
}
}
}

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 {
$WP::«»SetWallpaper( (Convert-Path $Path), $Style )

FuncCheckService -ServiceName \"Serveur\"

FuncCheckService -ServiceName \"Telnet\"


if($global:b -eq 0){
[string]$path= \"C:\vert.bmp\"
$WP::«»SetWallpaper( (Convert-Path $Path), $Style )
}else{
[string]$path= \"C:\rouge.bmp\"
$WP::«»SetWallpaper( (Convert-Path $Path), $Style )
}
}[/code:1]


Voici l'erreur rencontré:

Impossible d'effectuer un cast d'un objet de type 'System.Object[]' en type 'System.Type'.
Au niveau de ligne : 1 Caractère : 20
+ C:\Scripts\fond.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [fond.ps1], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException,fond.ps1



Une petite idée sur mon erreur?

Merci pour votre aide

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.037 secondes
Propulsé par Kunena