powershell远程创建ini文件

时间:2015-04-28 01:16:25

标签: powershell

我正在尝试创建一个脚本来通过powershell创建一个ini文件来禁用Windows UAC。

$functionText = @"`[Options`]
UpdateKey=04/28/2015 12:50:27 AM
WINDOW_LEFT=258
WINDOW_TOP=149
WINDOW_WIDTH=666
WINDOW_HEIGHT=519
WINDOW_MAX=0
BackupDir=C:\Windows\System32
UpdateCheck=1
Language=1033
(App)Sun Java=False
NewVersion=5.05.5176
SkipUAC=1
FinderInclude1=PATH|C:\|*.*|RECURSE
FinderInclude2=PATH|D:\|*.*|RECURSE
FinderIncludeStates=1|1
I see SkipUAC=1
ShowCleanWarning=False
ShowFirefoxCleanWarning=False
WipeFreeSpaceDrives=C:\
RunICS=0
CookiesToSave=*.piriform.com|google.com
"@

New-Item c:\Program Files\Ccleaner\Ccleaner.ini -type file -force -value $functionText

我一直在源文本中收到无法识别的令牌。 在C:\ PROGRA~3 \ BEANYW~1 \ Scripts \ 2480_C~1 \ ~SC52F~1.PS1:1 char:17 + $ functionText =<<<< @" [Options]     + CategoryInfo:ParserError:(:) [],ParseException     + FullyQualifiedErrorId:UnrecognizedToken

我尝试在选项周围添加转义字符,看看是否会这样做 - 我认为这个问题围绕着[选项]

1 个答案:

答案 0 :(得分:4)

如果你想使用here-string,请输入@"在一条线上。

$functionText = @"
[Options]
UpdateKey=04/28/2015 12:50:27 AM
WINDOW_LEFT=258
WINDOW_TOP=149
WINDOW_WIDTH=666
WINDOW_HEIGHT=519
WINDOW_MAX=0
BackupDir=C:\Windows\System32
UpdateCheck=1
Language=1033
(App)Sun Java=False
NewVersion=5.05.5176
SkipUAC=1
FinderInclude1=PATH|C:\|*.*|RECURSE
FinderInclude2=PATH|D:\|*.*|RECURSE
FinderIncludeStates=1|1
I see SkipUAC=1
ShowCleanWarning=False
ShowFirefoxCleanWarning=False
WipeFreeSpaceDrives=C:\
RunICS=0
CookiesToSave=*.piriform.com|google.com
"@

New-Item "C:\Program Files\Ccleaner\Ccleaner.ini" -type file -force -value $functionText

here-string的优点是你不必转义字符串中的任何内容。因此,如果有单引号或双引号则无关紧要。只要字符串'" @'在你自己的ini文件代码中,它本身并不存在。

阅读more about here-strings

另外,如上面的示例所示,您需要在文件路径周围加上引号。