powershell 调用winapi读写ini文件

#加载winapi
$ini = Add-Type -memberDefinition @"
[DllImport("Kernel32")]
public static extern long WritePrivateProfileString (
string section ,
string key , 
string val , 
string filePath );
[DllImport("Kernel32")]
public static extern int GetPrivateProfileString (
string section ,  
string key , 
string def , 
StringBuilder retVal ,  
int size , 
string filePath ); 
"@ -passthru -name MyPrivateProfileString -UsingNamespace System.Text


#定义配置
$section="server"
$filePath=".\test.ini"
$key="ip"
$val="192.168.0.1"
$retVal=New-Object System.Text.StringBuilder(32)

#生成或修改配置文件
$null=$ini::WritePrivateProfileString($section,$key,$val,$filePath)

#查看配置文件
$null=$ini::GetPrivateProfileString($section,$key,"",$retVal,32,$filePath)
Write-Host $key "=" $retVal.tostring()

你可能感兴趣的:(powreshell)