相关疑难解决方法(0)

将小的 PS 脚本转换为 .BATch 文件中的长行

我有这个 PowerShell 代码,是从这个问题的答案中得到的;它显示了运行 PS 代码的 cmd.exe 窗口的位置/尺寸:

$WindowFunction,$RectangleStruct = Add-Type -MemberDefinition @'
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}
'@ -Name "type$([guid]::NewGuid() -replace '-')" -PassThru

$MyWindowHandle = (Get-Process -Id (Get-WmiObject Win32_Process -Filter "ProcessId=$PID").ParentProcessId).MainWindowHandle

$WindowRect = New-Object -TypeName $RectangleStruct.FullName
$null = $WindowFunction::GetWindowRect($MyWindowHandle,[ref]$WindowRect)

Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom
Run Code Online (Sandbox Code Playgroud)

当我从命令行在 .ps1 脚本中运行此代码时,它可以正常工作:

C:\Users\Antonio\Documents\test> powershell Set-ExecutionPolicy …
Run Code Online (Sandbox Code Playgroud)

windows powershell cmd batch-file

3
推荐指数
1
解决办法
1194
查看次数

如何压缩PowerShell脚本以适合单行

快问.我正在尝试编写以下PowerShell脚本,但我希望它适合单行:

$app = New-Object -comobject Excel.Application
$wb1 = $app.Workbooks.Open("C:\xampp\upload_files\Launchpad.xlsm")
$app.Run("Refresh")
$wb1.Close($false)
$app.Quit()
Run Code Online (Sandbox Code Playgroud)

伪代码看起来像这样:

$app = New-Object -comobject Excel.Application AND $wb1 = $app.Workbooks.Open AND "C:\xampp\upload_files\Launchpad.xlsm") AND $app.Run("Refresh") AND $wb1.Close($false) AND $app.Quit()
Run Code Online (Sandbox Code Playgroud)

我想要在一行上的原因是因为我想直接在Windows任务计划程序的"参数"框中插入参数.原因是由于某些原因脚本被禁用(例如我无法调用.ps1文件......)

我知道这仍然有效,因为我已经有一个"单行"PS脚本运行.语法是什么样的?

亲切的问候,G.

windows powershell

2
推荐指数
1
解决办法
9435
查看次数

标签 统计

powershell ×2

windows ×2

batch-file ×1

cmd ×1