Question [Résolu] Exécution de commandes sur server distant

Plus d'informations
il y a 16 ans 1 semaine #6329 par brutosaure
Bonjour,

J'aimerais pourvoir exécuter une commande sur un serveur distant; qui va permettre de me sortir la liste de tout les fichiers d'un répertoire donné entre telle et telle date :

[code:1]
PS > ./monscript -startdate \"4/3/2010:00:01\" -endate \"7/3/2010:23:59\" -path \"D:/Logs/IIS\" -server name_server[/code:1]
Pour ce faire je déclare mes variables
[code:1]
param($startDate,$endDate,$path,$server)
$user = ./import-credential.ps1 chemin_mon_fichier_txt[/code:1]
Ensuite je stocke ma requête que je veux lancer sur le serveur distant dans une variable
[code:1]
$command=\"Get-ChildItem -path $path -recurse | ?{(`$_.LastWriteTime -ge [datetime]::«»ParseExact($startDate,'d/M/yyyy:HH:mm',`$null)) -and (`$_.LastWriteTime -le [datetime]::«»ParseExact($endDate,'d/M/yyyy:HH:mm',`$null))}\"[/code:1]
Je check si c'est bien la syntaxe que je veux lancer sur le serveur

[code:1]
#check contain $command
write-host $command [/code:1]
et c'est bien exactement ce que je veux qui s'affiche

[code:1]
Get-ChildItem -path D:\Logs\IIS -recurse | ?{($_.LastWriteTime -ge [datetime]::«»ParseExact(4/3/2010:00:01,'d/M/yyyy:HH:mm',$null)) -and ($_.LastWriteTime -le [datetime]::«»Par
seExact(07/3/2010:23:59,'d/M/yyyy:HH:mm',$null))}[/code:1]

Puis je la lance sur le serveur
[code:1]
invoke-command -credential $user -Computername $server -Scriptblock {param($pCommand) Invoke-Expression -command $pCommand} -ArgumentList $command[/code:1]

Mais quand cette partie là ce lance j'ai une erreur

Missing ')' in method call.
+ CategoryInfo : ParserError: (CloseParenToken:TokenId) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall,Microsoft.PowerShell.Commands.InvokeExpressionCommand


Pourtant que je déclare mes paramètres sur le serveur et que je lance ma commande ca fonctionne.

Je vois pas ou ce trouve le problème.
Si quelqu'un à une idée, je suis preneur.

Message édité par: Arnaud, à: 10/03/10 16:13<br><br>Message édité par: Arnaud, à: 18/03/10 08:38

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

Plus d'informations
il y a 16 ans 1 semaine #6330 par Laurent Dardenne
Salut,
il faut utiliser des guillemets lors de substitutions de chaînes :
[code:1]
$startdate=\&quot;4/3/2010:00:01\&quot;
$endDate=\&quot;7/3/2010:23:59\&quot;
$path=\&quot;C:\Program Files\truc\&quot;
$command=@\&quot;
Get-ChildItem -path \&quot;$path\&quot; -recurse |
Where {(`$_.LastWriteTime -ge [datetime]::«»ParseExact(\&quot;$startDate\&quot;,'d/M/yyyy:HH:mm',`$null)) -and
(`$_.LastWriteTime -le [datetime]::«»ParseExact(\&quot;$endDate\&quot;,'d/M/yyyy:HH:mm',`$null))
}
\&quot;@
$Command
Invoke-Expression -command $Command
[/code:1]

Tutoriels PowerShell

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

Plus d'informations
il y a 16 ans 1 semaine #6332 par brutosaure
Apparement ca ne fonctionne pas quand je mets mes variables entre \&quot;\&quot;.

[code:1]
$command=\&quot;Get-ChildItem -path \&quot;$path\&quot; -recurse | Where {(`$_.LastWriteTime -ge [datetime]::«»ParseExact(\&quot;$startDate\&quot;,'d/M/yyyy:HH:mm',`$null)) -and (`$_.LastWriteTime -le [datetime]::«»ParseExact(\&quot;$endDate\&quot;,'d/M/yyyy:HH:mm',`$null))}\&quot;[/code:1]
J'ai cette erreur

[code:1]Unexpected token 'Get-ChildItem' in expression or statement.
At C:\Data\Dev\PowerShell\Scripts\scptSearchLogIIS2.ps1:15 char:25
+ $command=\&quot;\&quot;Get-ChildItem &lt;&lt;&lt;&lt; -path \&quot;$path\&quot; -recurse | Where {(`$_.LastWriteTime -ge [datetime]::«»ParseExact(\&quot;$startDate\&quot;,'d/M/yyyy:HH:mm',`$null)) -and (`$_.LastWriteTi
me -le [datetime]::«»ParseExact(\&quot;$endDate\&quot;,'d/M/yyyy:HH:mm',`$null))}\&quot;\&quot;
+ CategoryInfo : ParserError: (Get-ChildItem:«»String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken[/code:1]

et quand je procède comme ceci

[code:1]
$command=\&quot;Get-ChildItem -path $path -recurse | Where {(`$_.LastWriteTime -ge [datetime]::«»ParseExact($startDate,'d/M/yyyy:HH:mm',`$null)) -and (`$_.LastWriteTime -le [datetime]::«»ParseExact($endDate,'d/M/yyyy:HH:mm',`$null))}\&quot;[/code:1]

J'ai bien la ligne que je veux faire passer à invoke-expression

[code:1]Get-ChildItem -path D:\Logs\IIS -recurse | Where {($_.LastWriteTime -ge [datetime]::«»ParseExact(4/3/2010:00:01,'d/M/yyyy:HH:mm',$null)) -and ($_.LastWriteTime -le [datetime]
::«»ParseExact(07/3/2010:23:59,'d/M/yyyy:HH:mm',$null))}[/code:1]
C'est exactement cette commande là que je veux passer

Edité par Arnaud : Brutosaure, utilise la section Code pour poster ton code; ainsi les smileys n'apparaitrons plus.<br><br>Message édité par: Arnaud, à: 10/03/10 16:19

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

Plus d'informations
il y a 16 ans 1 semaine #6336 par Arnaud Petitjean
Salut Brutosaure,

Pourquoi ne pas procéder autrement, en faisant ton script pour s'exécuter localement puis en lançant l'exécution de ce script sur une machine distante.

Toujours avec la commande Invoke-Command :
[code:1]
PS &gt; Invoke-Command -FilePath c:\scripts\monscript.ps1 -ComputerName MonServeur
[/code:1]

Le chemin du paramètre -FilePath est le chemin local de la machine à partir de laquelle tu lances la commande.

Arnaud

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

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

Plus d'informations
il y a 16 ans 1 semaine #6348 par Laurent Dardenne
brutosaure écrit:

Apparement ca ne fonctionne pas quand je mets mes variables entre \&quot;\&quot;.

Dans ton cas tu n'utilises pas de chaîne littérale, Here-String, il est donc normal d'avoir cette erreur.
Soit tu utilises une here-string soit tu échappes les guillemets.

Tutoriels PowerShell

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

Plus d'informations
il y a 16 ans 1 semaine #6354 par brutosaure
bonjour et merci encore pour votre aide, en fait ca fonctionne quand je passe directement toute ma commande à invoke-command et non par le biais d'une variable
[code:1]
param($startDate,$endDate,$path,$server)

#Import the crypted text file for login and password
$user = ./import-credential.ps1 C:\chemin.txt

#Test if the variables exist
if (!($startDate) -or !($endDate) -or !($path) -or !($server))
{
write-host \&quot;Please introduce the server name, the start date, end date and the path\&quot;
write-host \&quot; \&quot;
write-host \&quot;The options are -path -server -stardate -enddate\&quot;
write-host \&quot; \&quot;
write-host \&quot;Exemple ./scptSearchLog.ps1 -startdate `\&quot;4/3/2010:00:01`\&quot; -enddate `\&quot;07/3/2010:23:59`\&quot; -path c:\ -server xxxxx\&quot;
}
else
{
$command=\&quot;Get-ChildItem -path $path -recurse | where-object{(`$_.LastWriteTime -ge [datetime]::«»ParseExact($startDate,'d/M/yyyy:HH:mm',`$null)) -and (`$_.LastWriteTime -le [datetime]::«»ParseExact($endDate,'d/M/yyyy:HH:mm',`$null))}\&quot;
invoke-command -credential $user -Computername $server -scriptblock{param($pCommand) invoke-command -scriptblock{$pCommand | gm}} -ArgumentList $command
}
[/code:1]
Là cela m'affiche le contenue de ma variable $pCommand

Mais si je fais ceci:
[code:1]
invoke-command -credential $user -Computername $server -scriptblock{param($paraStartdate, $paraEnddate, $paraPath) invoke-command -scriptblock{Get-ChildItem -path $paraPath -recurse | Where-object{($_.LastWriteTime -ge [datetime]::«»ParseExact($paraStartDate,'d/M/yyyy:HH:mm',$null)) -and ($_.LastWriteTime -le [datetime]::«»ParseExact($paraEndDate,'d/M/yyyy:HH:mm',$null))}}} -ArgumentList $startdate, $enddate, $path
[/code:1]

Alors j'ai bien le résultat que je cherche
[code:1]invoke-command -scriptblock{$pcommand}[/code:1]Pq invoke-command ne veut pas m'exécuter la commande et m'affiche son contenu?

Message édité par: brutosaure, à: 12/03/10 11:50<br><br>Message édité par: brutosaure, à: 12/03/10 12:33

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

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