![](/img/trans.png)
[英]Script Works in Powershell CLI but not in ISE or as a PS1
[英]Why does my script work in powershell ISE but not when it is in a .ps1 file?
它曾經工作過,但現在在打開后不到一秒鍾突然終止。 我將執行策略設置為不受限制並重新安裝了Windows,但仍然無法正常工作。.ps1在任務管理器中顯示1秒鍾,然后使用.vbs運行Windows安全運行狀況服務,然后消失了: https:/ /i.imgur.com/VNX7NKx.png
這是腳本(其目的是顯示通知消息):
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$notifyobj = New-Object System.Windows.Forms.NotifyIcon
$notifyobj.icon = "c:/users/work/Pictures/icon.ico"
$notifyobj.BalloonTipTitle = "New Message"
$notifyobj.BalloonTipText = "C"
$notifyobj.Visible = $True
$notifyobj.ShowBalloonTip(1000)
$notifyobj.Dispose()
有關此線程的更多信息。
毫無疑問,您應該沒有任何理由必須重新安裝Windows。 因此,其他因素正在影響這一點。 雖然很難說。
試試這個版本,看看是否有任何/更多的成功。 它采用不同的方法,但是可以在我的系統上使用。
Function Show-Notification
{
Param
(
[string]$MessageType,
[string]$MessageText,
[string]$MessageTitle
)
#load Windows Forms and drawing assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#define an icon image pulled from PowerShell.exe
$Icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe))
$Notify = New-Object System.Windows.Forms.NotifyIcon
$Notify.icon = $Icon
$Notify.visible = $True
#define the tool tip icon based on the message type
switch ($messagetype)
{
"Error" {$MessageIcon = [System.Windows.Forms.ToolTipIcon]::Error}
"Info" {$MessageIcon = [System.Windows.Forms.ToolTipIcon]::Info}
"Warning" {$MessageIcon = [System.Windows.Forms.ToolTipIcon]::Warning}
Default {$MessageIcon = [System.Windows.Forms.ToolTipIcon]::None}
}
#display the balloon tipe
$Notify.showballoontip($Notification_timeout,$MessageTitle,$MessageText,$MessageType)
}
Show-Notification -MessageType Info -MessageText 'some message' -MessageTitle 'New Alert'
更新資料
修改發布的代碼以匹配我的一些項目,可以使代碼按預期在ISE控制台中工作。
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$notifyobj = New-Object System.Windows.Forms.NotifyIcon
# I don't have your icon, so, using what I know I can reach
$notifyobj.Icon = [system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe))
$notifyobj.BalloonTipTitle = "New Message"
$notifyobj.BalloonTipText = "C"
$notifyobj.Visible = $True
$notifyobj.ShowBalloonTip(1000)
$notifyobj.Dispose()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.