简体   繁体   English

powershell.exe -ex旁路-命令

[英]powershell.exe -ex bypass -command

I have a working powershell script, i Need to convert the entire script to run in single line of code with powershell.exe -ex bypass -command .... 我有一个工作正常的powershell脚本,我需要使用powershell.exe -ex旁路-command ...将整个脚本转换为在一行代码中运行。

The reason in the background is script should not run as .ps1 file instead i can run as single command. 后台原因是脚本不应以.ps1文件运行,而我可以作为单个命令运行。 Am looking help in this... am quite new to powershell..but tried to manage the script below.. i need to convert it to run from command as single line of code.. 我正在寻找帮助...对于powershell来说是一个相当新的东西。但是试图管理下面的脚本。我需要将其转换为从命令运行为单行代码。

    # Config
$logFileName = "Application" # Add Name of the Logfile (System, Application, etc)
$path = "c:\Intel\" # Add Path, needs to end with a backsplash

# do not edit
$exportFileName = $logFileName + (get-date -f yyyyMMdd) + ".evt"
$logFile = Get-WmiObject Win32_NTEventlogFile | Where-Object {$_.logfilename -eq $logFileName}
$logFile.backupeventlog($path + $exportFileName)

You don't need -ex (short for -ExecutionPolicy ) if you're not using a file, because it only applies to files. 如果您不使用文件,则不需要-ex-ExecutionPolicy ),因为它仅适用于文件。

To make it one line, you basically replace newlines with ; 要使其成为一行,您基本上用;替换了换行符; .

But -Command isn't the best idea for this. 但是-Command并不是最好的主意。 You're going to have to be careful about properly escaping all the quotes and pipes throughout your code. 您将必须谨慎地正确转义整个代码中的所有引号和管道。

You can look into -EncodedCommand , whereby you Base64 encode your code, and pass it all as one string. 您可以查看-EncodedCommand ,从而对Base64进行编码,并将其全部作为一个字符串传递。

If you check powershell.exe /? 如果检查powershell.exe /? it has an example at the bottom: 它在底部有一个示例:

 # To use the -EncodedCommand parameter: $command = 'dir "c:\\program files" ' $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) powershell.exe -encodedCommand $encodedCommand 

If you could explain more about your reasons for not wanting to use a file, it may be helpful in getting answers that are more appropriate for your situation. 如果您可以详细说明不希望使用文件的原因,则可能有助于获取更适合您情况的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM