Search code examples
powershelljscript

Powershell ' add-type -Language JScript '


Add-Type @'
class Network {
    var WsNetwork,str;
    function getPcName(){
        WsNetwork = WScript.CreateObject("WScript.Network");
        str="PC name: "+WsNetwork.ComputerName;
        return str;
    }
}
'@ -Language JScript

$PcName = [Network]::new()
$PcName.getPcName()

How is correct to add this JScript code? Thats not compiles

UPD--------------------

This works:

Add-Type @'
class Network {
    var WsNetwork;
    
    function getPcName(){
        WsNetwork= new ActiveXObject("WScript.Network"); 
        return WsNetwork;
    } 
}
'@ -Language JScript

$PcName = [Network]::new()
$PcName.getPcName().ComputerName

But why return WsNetwork.ComputerName; not works?


Solution

  • It's Jscript.net that's supported, as well as VB.net and F#.net. The 5.1 docs have a examples. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-5.1#example-6--add-a-class-with-jscript-net