iT邦幫忙

0

powershell 找不到類型

各位大大好
小弟最近在學寫POWERSHELL 在一段程式碼裡面我希望呼叫VBS 輸入介面 並且返回USER輸入
如下圖 但如果我把它寫在CLASS 裡面會出現以下錯誤

https://ithelp.ithome.com.tw/upload/images/20200313/20115672nGJ662A0eb.png

但沒寫在CLASS 裡面就不會有錯誤跑出來

https://ithelp.ithome.com.tw/upload/images/20200313/20115672hKwi9NwYUn.png
請各位可以指引個方向或者跟我說我需要知道些什麼 謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
froce
iT邦大師 1 級 ‧ 2020-03-13 12:59:01
class test{
    [string]input(){
        [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
        $inputbox = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "Lastname")
        return $inputbox
    }
}

$t = [test]::new()

$t.input()

你要load VB的模組要先引入,在class外面不知道為啥沒報錯。

0
zero
iT邦好手 1 級 ‧ 2020-03-13 16:17:46

載入的地方有問題,你在=號的地方載入VB當然會錯

$是Powershell宣告變數的符號

正常使用是$x="Hello"這樣

不會有人在=右邊做任何載入模組的宣告

你應該要像這樣做

 Add-Type -AssemblyName Microsoft.VisualBasic
 
 $Message = [Microsoft.VisualBasic.Interaction]::InputBox("test1", "test2")
 
 return $Message

我要發表回答

立即登入回答