简体   繁体   English

ExtractAssociatedIcon - 在 PowerShell ISE 中工作正常,但在控制台中不起作用

[英]ExtractAssociatedIcon - In PowerShell ISE Work's fine, But in Console Doesn't Work

I have a Powershell Script, Which Contains:我有一个 Powershell 脚本,其中包含:

$Path = 'c:\windows\system32\notepad.exe'
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)

$Icon.ToBitmap().Save('C:\icon.png')

I write those script in the ' PwerShell ISE ' when I run, it work's fine, and not problem show's up.当我运行时,我在“ PwerShell ISE ”中编写了这些脚本,它运行良好,并且没有出现问题。

图片


But when saving those script, and run by right click on ' icon.ps1 ' then ' Run With PowerShell ' it will get this error:但是当保存这些脚本并通过右键单击“ icon.ps1 ”然后“ Run With PowerShell运行时,它会得到这个错误:

PS D:\My PowerShell\tmp> .\icon.ps1
Unable to find type [System.Drawing.Icon].
At D:\My PowerShell\tmp\icon.ps1:2 char:9
+ $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
+         ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Drawing.Icon:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

You cannot call a method on a null-valued expression.
At D:\My PowerShell\tmp\icon.ps1:4 char:1
+ $Icon.ToBitmap().Save('C:\icon.png')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You need to load the System.Drawing assembly before you run the rest of the script.在运行脚本的其余部分之前,您需要加载 System.Drawing 程序集。 At the beginning of the script, put this line:在脚本的开头,放置这一行:

Add-Type -AssemblyName System.Drawing

This will load the assembly first, allowing the rest of the script to access the methods and members of that assembly.这将首先加载程序集,允许脚本的其余部分访问该程序集的方法和成员。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.1 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.1

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

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