Fondamentalement, j'ai un script, qui essaie de lire le contenu d'un fichier de configuration appelé commands.ini qui redirige vers un fichier texte qui contient une commande à exécuter.comment exécuter un script qui lit un fichier .ini pour exécuter une commande à partir d'un fichier texte
Voici le contenu du fichier de mon script:
Param(
[Parameter(Mandatory=$true)][string]$config
)
#Function to read *.ini file and populate an hashtable
Function Get-IniFile ($file) {
$ini = @{}
switch -regex -file $file {
"^\[(.+)\]$" {
$section = $matches[1].Trim()
$ini[$section] = @{}
}
"^\s*([^#].+?)\s*=\s*(.*)" {
$name,$value = $matches[1..2]
# skip comments that start with semicolon:
if (!($name.StartsWith(";"))) {
$ini[$section][$name] = $value.Trim()
}
}
}
return $ini
}
# Getting parameters from *.ini file
$ini = Get-IniFile($config)
$commands_file = $ini['COMMANDS']['commands_file']
# In case any of the files containing the commands: EXIT.
if (Test-Path $commands_file) {
[string[]]$commands = Get-Content $commands_file
} else {
Write-Output "# ERROR: cannot read commands_file. Please check configuration. Exiting..."
Break
}
# This is the command I am trying to run among the various other similar command just to read the ini file
# and execute the command from the text file which is directed to from the ini file
invoke-expression $commands_file[0]
J'ai aussi changé un peu et utilisé commande Invoke-commande, mais ne fonctionne pas.
Voici le contenu du fichier commands.ini:
[COMMANDS]
; this is where the file to the list of commands to execute will be mentioned
;
commands_file = C:\test\Test\find\commands.txt
et le contenu du fichier commands.txt:
« Get-Process | Where-Object {$ _. Nom-like "a *"} '
mais je reçois toujours la même erreur, peu importe les changements que je fais, je suis sûr que quelque chose ne va pas comme on appelle mes tables de hachage ou quelque chose mais je ne peux pas comprendre ce qui cause exactement cette erreur.
erreur affiché dans PowerShell: Error
C : The term 'C' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C
+ ~
+ CategoryInfo : ObjectNotFound: (C:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
S'il vous plaît conseiller les gars, merci à l'avance.
Sera également très apprécié si quelqu'un peut m'expliquer la partie "Obtenir des paramètres de fichier * .ini" dans le script dans les détails, j'ai les connaissances de base PowerShell.
Désolé Chris pour le commentaire très tard, mais que votre commande a travaillé et fait mon travail, alors je suis devenu occupé,; p merci mon pote :) –