PowerShell 创建网络打印机的快捷方式 [英] PowerShell create shortcut to network printer

查看:76
本文介绍了PowerShell 创建网络打印机的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建网络打印机的快捷方式.

I am trying to create a shortcut to the network printer.

这是 PowerShell 脚本:

Here is the PowerShell script:

$DesktopPath = [Environment]::GetFolderPath("Desktop")
$create_shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut
$s = $create_shortcut.invoke("$DesktopPath\ConnectPrinter.lnk")
$s.TargetPath = "C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /y /in /q /n \\192.168.1.205\printer1"
$s.IconLocation = "imageres.dll,76"
$s.Save() 

当我运行脚本时,它会生成快捷方式,但是快捷方式的目标"部分出现,所有交换机上的正斜杠更改为反斜杠,其中一个反斜杠从 IP 地址中删除,引号添加到开头&整行结束)

When I run the script, it generates the shortcut however the "Target" part of the shortcut appears with Forward slash changed to backward slash on all switches, one of the back slash removed from the IP Address and quotes added to the start & end of the whole line)

"C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry \y \in \q \n \192.168.1.205\printer1"

我需要对脚本进行哪些更改才能生成正确的目标,如下所示:

What changes I need to do on the script so that it will generate the correct Target like below:

C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /y /in /q /n "\\192.168.1.205\printer1"

任何帮助将不胜感激,谢谢.

Any help would be much appreciated, thank you.

推荐答案

为了复用性,前段时间写了一个辅助函数来新建快捷方式文件.
作为奖励,它还允许您设置以管理员身份运行"复选标记(尽管此问题不需要)

For reusability, I wrote a helper function some time ago to create new shortcut files.
As a bonus, it also allows you to set the 'Run as Administrator' checkmark (not needed for this question though)

function New-Shortcut {
    [CmdletBinding()]  
    Param (   
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$TargetPath,                # the path to the executable
        # the rest is all optional
        [string]$ShortcutPath = (Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'New Shortcut.lnk'),
        [string[]]$Arguments = $null,       # a string or string array holding the optional arguments.
        [string[]]$HotKey = $null,          # a string like "CTRL+SHIFT+F" or an array like 'CTRL','SHIFT','F'
        [string]$WorkingDirectory = $null,  
        [string]$Description = $null,
        [string]$IconLocation = $null,      # a string like "notepad.exe, 0"
        [ValidateSet('Default','Maximized','Minimized')]
        [string]$WindowStyle = 'Default',
        [switch]$RunAsAdmin
    ) 
    switch ($WindowStyle) {
        'Default'   { $style = 1; break }
        'Maximized' { $style = 3; break }
        'Minimized' { $style = 7 }
    }
    $WshShell = New-Object -ComObject WScript.Shell

    # create a new shortcut
    $shortcut             = $WshShell.CreateShortcut($ShortcutPath)
    $shortcut.TargetPath  = $TargetPath
    $shortcut.WindowStyle = $style
    if ($Arguments)        { $shortcut.Arguments = $Arguments -join ' ' }
    if ($HotKey)           { $shortcut.Hotkey = ($HotKey -join '+').ToUpperInvariant() }
    if ($IconLocation)     { $shortcut.IconLocation = $IconLocation }
    if ($Description)      { $shortcut.Description = $Description }
    if ($WorkingDirectory) { $shortcut.WorkingDirectory = $WorkingDirectory }

    # save the link file
    $shortcut.Save()

    if ($RunAsAdmin) {
        # read the shortcut file we have just created as [byte[]]
        [byte[]]$bytes = [System.IO.File]::ReadAllBytes($ShortcutPath)
        # $bytes[21] = 0x22      # set byte no. 21 to ASCII value 34
        $bytes[21] = $bytes[21] -bor 0x20 #s et byte 21 bit 6 (0x20) ON
        [System.IO.File]::WriteAllBytes($ShortcutPath, $bytes)
    }

    # clean up the COM objects
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

你可以这样使用

$props = @{
    'ShortcutPath' = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'ConnectPrinter.lnk'
    'TargetPath'   = 'C:\Windows\System32\rundll32.exe'
    'Arguments'    = 'printui.dll,PrintUIEntry', '/y', '/in', '/q', '/n', '\\192.168.1.205\printer1'
    'IconLocation' = 'imageres.dll,76'
    'Description'  = 'Connect to Printer1'
}

New-Shortcut @props

这篇关于PowerShell 创建网络打印机的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆