基本的に私はスクリプトファイルを持っています。このファイルは、コマンドファイルと呼ばれるテキストファイルにリダイレクトされるcommands.iniという内容です。実行するコマンドが含まれています。.iniファイルを読み込んでテキストファイルからコマンドを実行するスクリプトを実行する方法
は、ここに私のスクリプト.ps1というファイルの内容です:
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]
私も少し周りを変更し、Invoke-Commandコマンドコマンドを使用しているが、動作しません。
ここ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
とcommands.txtファイルの内容:
「ゲット・プロセスを|
でも、私は何度も変更を加えても同じエラーが表示されますが、私のハッシュテーブルが呼び出される方法が間違っていると確信しています。または何かが私は正確にこのエラーの原因を把握することはできません。 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
、事前に感謝をみんなにアドバイスしてください。
「* .iniファイルからのパラメータの取得」の部分を私に説明することができれば、非常に高く評価されます。詳細は、PowerShellの基本知識があります。
invoke-expression $commands_file[0]
は、この時点での$ commands_fileは、文字列が含まれ、 "C:\テスト\テスト\検索\のcommands.txt"
非常に遅いコメントのクリスマス申し訳ありませんが、あなたのコマンドが働いて私の仕事をしたので、私は忙しくなりました; ありがとうございました:) –