2014-10-11 53 views
0

我想有Windows的計劃任務(XML)文件通過PowerShell中創建使用下面的命令:回聲原始XML與PowerShell的文件 - 行情被刪除

$userName = $ENV:USERNAME 
$compName = $ENV:COMPUTERNAME 

echo "<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
    <RegistrationInfo> 
    <Date>2014-10-09T14:28:16</Date> 
    <Author>$userName</Author> 
    </RegistrationInfo> 
    <Triggers> 


    <TimeTrigger> 
     <Repetition> 
     <Interval>PT2M</Interval> 
     <StopAtDurationEnd>false</StopAtDurationEnd> 
     </Repetition> 
     <StartBoundary>2014-10-09T14:28:00</StartBoundary> 
     <Enabled>true</Enabled> 
    </TimeTrigger> 
    </Triggers> 
    <Principals> 
    <Principal id="Author"> 
     <UserId>$compName\$userName</UserId> 
     <LogonType>InteractiveToken</LogonType> 
     <RunLevel>LeastPrivilege</RunLevel> 
    </Principal> 
    </Principals> 
    <Settings> 
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> 
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries> 
    <AllowHardTerminate>true</AllowHardTerminate> 
    <StartWhenAvailable>false</StartWhenAvailable> 
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
    <IdleSettings> 
     <StopOnIdleEnd>false</StopOnIdleEnd> 
     <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings> 
    <AllowStartOnDemand>true</AllowStartOnDemand> 
    <Enabled>true</Enabled> 
    <Hidden>false</Hidden> 
    <RunOnlyIfIdle>false</RunOnlyIfIdle> 
    <WakeToRun>true</WakeToRun> 
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit> 
    <Priority>7</Priority> 
    </Settings> 
    <Actions Context="Author"> 
    <Exec> 
     <Command>C:\Windows\System32\WScript.exe</Command> 
     <Arguments>//Nologo //B C:\Microsoft\Windows\Desktop\Initialize.vbs</Arguments> 
    </Exec> 
    </Actions> 
</Task>" >> dog.xml 

但是輸出文件已經失去了它的所有報價如下所示:

<?xml version= 
1.0 encoding=UTF-16?> 
<Task version=1.2 xmlns=http://schemas.microsoft.com/windows/2004/02/mit/task> 
    <RegistrationInfo> 
    <Date>2014-10-09T14:28:16</Date> 
    <Author>Xaptoxa</Author> 
    </RegistrationInfo> 
    <Triggers> 
    <TimeTrigger> 
     <Repetition> 
     <Interval>PT2M</Interval> 
     <StopAtDurationEnd>false</StopAtDurationEnd> 
     </Repetition> 
     <StartBoundary>2014-10-09T14:28:00</StartBoundary> 
     <Enabled>true</Enabled> 
    </TimeTrigger> 
    </Triggers> 
    <Principals> 
    <Principal id=Author> 
     <UserId>GIBSON\Xaptoxa</UserId> 
     <LogonType>InteractiveToken</LogonType> 
     <RunLevel>LeastPrivilege</RunLevel> 
    </Principal> 
    </Principals> 
    <Settings> 
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> 
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries> 
    <AllowHardTerminate>true</AllowHardTerminate> 
    <StartWhenAvailable>false</StartWhenAvailable> 
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
    <IdleSettings> 
     <StopOnIdleEnd>false</StopOnIdleEnd> 
     <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings> 
    <AllowStartOnDemand>true</AllowStartOnDemand> 
    <Enabled>true</Enabled> 
    <Hidden>false</Hidden> 
    <RunOnlyIfIdle>false</RunOnlyIfIdle> 
    <WakeToRun>true</WakeToRun> 
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit> 
    <Priority>7</Priority> 
    </Settings> 
    <Actions Context=Author> 
    <Exec> 
     <Command>C:\Windows\System32\WScript.exe</Command> 
     <Arguments>//Nologo //B C:\Microsoft\Windows\Desktop\Initialize.vbs</Arguments> 
    </Exec> 
    </Actions> 
</Task> 

我也嘗試過使用單引號嵌套,但這會導致變量失敗。除了嵌套外,我還嘗試了各種Powershell技術,如Out-File CmdLet等。

回答

1

您可以使用此處的字符串代替。這對我有用(刪除部分XML以節省空間):

$userName = $ENV:USERNAME 
$compName = $ENV:COMPUTERNAME 

echo @" 
<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
    <RegistrationInfo> 
    <Date>2014-10-09T14:28:16</Date> 
    <Author>$userName</Author> 
    </RegistrationInfo> 
    <Triggers> 
    ... 
    <Actions Context="Author"> 
    <Exec> 
     <Command>C:\Windows\System32\WScript.exe</Command> 
     <Arguments>//Nologo //B C:\Microsoft\Windows\Desktop\Initialize.vbs</Arguments> 
    </Exec> 
    </Actions> 
</Task> 
"@ >> dog.xml 
+1

不錯的工作克里斯,歡呼! – shadowmoses 2014-10-11 12:44:42

0

或者,您可以轉義所有引號,以便它們正確傳遞。無論是Control + H - 全部替換爲記事本,還是使用正則表達式替換''',''''在每個引號前添加反引號。

echo "<?xml version=`"1.0`" encoding=`"UTF-16`"?> 
<Task version=`"1.2`" xmlns=`"http://schemas.microsoft.com/windows/2004/02/mit/task`"> 
    <RegistrationInfo> 
    <Date>2014-10-09T14:28:16</Date> 
    <Author>$userName</Author> 
    </RegistrationInfo> 
    <Triggers> 


    <TimeTrigger> 
     <Repetition> 
     <Interval>PT2M</Interval> 
     <StopAtDurationEnd>false</StopAtDurationEnd> 
     </Repetition> 
     <StartBoundary>2014-10-09T14:28:00</StartBoundary> 
     <Enabled>true</Enabled> 
    </TimeTrigger> 
    </Triggers> 
    <Principals> 
    <Principal id=`"Author`"> 
     <UserId>$compName\$userName</UserId> 
     <LogonType>InteractiveToken</LogonType> 
     <RunLevel>LeastPrivilege</RunLevel> 
    </Principal> 
    </Principals> 
    <Settings> 
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> 
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries> 
    <AllowHardTerminate>true</AllowHardTerminate> 
    <StartWhenAvailable>false</StartWhenAvailable> 
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
    <IdleSettings> 
     <StopOnIdleEnd>false</StopOnIdleEnd> 
     <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings> 
    <AllowStartOnDemand>true</AllowStartOnDemand> 
    <Enabled>true</Enabled> 
    <Hidden>false</Hidden> 
    <RunOnlyIfIdle>false</RunOnlyIfIdle> 
    <WakeToRun>true</WakeToRun> 
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit> 
    <Priority>7</Priority> 
    </Settings> 
    <Actions Context=`"Author`"> 
    <Exec> 
     <Command>C:\Windows\System32\WScript.exe</Command> 
     <Arguments>//Nologo //B C:\Microsoft\Windows\Desktop\Initialize.vbs</Arguments> 
    </Exec> 
    </Actions> 
</Task>" >> c:\dog.xml 
+0

謝謝關節鉗,知道的好把戲。 – shadowmoses 2014-10-12 09:32:39