我有一个PowerShell脚本,我想将输出重定向到一个文件.问题是我无法改变调用此脚本的方式.所以我做不到:
.\MyScript.ps1 > output.txt
Run Code Online (Sandbox Code Playgroud)
如何在执行期间重定向PowerShell脚本的输出?
我有一个监控图像文件夹的PowerShell脚本.我需要找到一种在计算机启动后自动运行此脚本的方法.
我已经尝试了以下方法,但我无法使其正常工作.
使用msconfig并将PowerShell脚本添加到启动,但我找不到该列表上的PowerShell脚本.
创建快捷方式并将其拖放到启动文件夹.没运气.
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)
或%SystemRoot%\ system32\WindowsPowerShell\v1.0\powershell.exe -File"C:\ Doc\Files\FileMonitor.ps1"
这是我的PowerShell脚本:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)我还尝试使用taskschd.msc添加基本任务.它仍然无法正常工作.
这是我发现的,也许这将有助于调试它.
如果我打开PowerShell窗口并在那里运行脚本,它就可以工作了.但如果我在CMD提示中运行它,
$folder = "C:\\Doc\\Files"
$dest = "C:\\Doc\\Files\\images"
$filter = "*.jpg"
$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
Start-Sleep -s 10
Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images
}
Run Code Online (Sandbox Code Playgroud)
不起作用.我不确定这是许可问题还是其他问题.
顺便说一下,我安装了PowerShell 3.0,如果输入$ host.version,它会在那里显示3.但我的powershell.exe似乎仍然是v1.0.
powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
Run Code Online (Sandbox Code Playgroud)在EC2实例上创建自定义AMI(图像)时遇到问题.如果我使用自定义引导程序/用户数据脚本启动Windows默认2012服务器实例,例如;
<powershell>
PowerShell "(New-Object System.Net.WebClient).DownloadFile('http://download.microsoft.com/download/3/2/2/3224B87F-CFA0-4E70-BDA3-3DE650EFEBA5/vcredist_x64.exe','C:\vcredist_x64.exe')"
</powershell>
Run Code Online (Sandbox Code Playgroud)
它将按预期工作并转到URL并下载文件,并将其存储在C:Drive上.
但是,如果我设置了一个Windows Server实例,然后从中创建一个映像,并将其存储为自定义AMI,然后使用完全相同的自定义用户数据脚本进行部署,它将无法正常工作.但是,如果我转到实例url(http://169.254.169.254/latest/user-data),它将显示脚本已成功导入但尚未执行.
检查错误日志后,我经常注意到这一点:
Failed to fetch instance metadata http://169.254.169.254/latest/user-data with exception The remote server returned an error: (404) Not Found.
Run Code Online (Sandbox Code Playgroud)