2016-08-03 10 views
1

基本的に私はスクリプトファイルを持っています。このファイルは、コマンドファイルと呼ばれるテキストファイルにリダイレクトされる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"

答えて

1

それは欠陥がある、この部分ですので、使用時に[0]へ最初の文字を選択することを指します(文字列はインデックスでアクセスできます)。最初の文字はCですので、Invoke-Expressionを実行しようとするとエラーメッセージが表示されます。

インデックス([0])を削除するだけでは不十分です。テキストファイルを開くだけで十分です。

Invoke-Expression (Get-Content $commands_file -Raw) 

あなたは単に、あなたがそれを呼び出すのではなく起動-式を心配して入手することができ、.ps1というに(iniファイルに)$ commands_fileを変更する場合があります:それはあなたが実行する必要があると思い立つようにそれを使用するには-コンテンツ。

iniファイルパーサーは比較的簡単です。一度に1行ずつiniファイルを読み込み、コンテンツをネストされたハッシュテーブル(キーと値のペア)に読み込みます。角括弧([something])で値が見つかるたびに、それは "セクション"ハッシュテーブルを作成します。キーと値(this = that)が出現するたびに、セクションの下に新しいエントリが追加されます。あなたはこのような構造で終わる:

@{ 
    'COMMANDS' = @{ 
     'commands_file' = 'C:\stuff\working\scratch\commands.txt' 
    } 
} 

のiniファイルではなく、昔ながらの非常に古い、これらの日で動作するように素敵なものです。 Json、CSV、およびXML形式は、正規表現に基づく解析ではあまり問題になりません。

+0

非常に遅いコメントのクリスマス申し訳ありませんが、あなたのコマンドが働いて私の仕事をしたので、私は忙しくなりました; ありがとうございました:) –

関連する問題