Run PowerShell without .NET Framework

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Kahlan Amnell Kahlan Amnell 1 year, 3 months ago.

  • Author
  • #27028
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    Can you still load .NET assemblies inside your PowerShell script even if .NET Framework is not installed? It confuses me because C:\Windows\Microsoft.NET\Framework is there with folders from versions 1 to 4 in there. But if I look at Programs and Features, Microsoft .NET Framework is not installed in there.

    I'm asking because in one of my environment where I have my script and .NET 4.0 is installed, it works fine. But in the environment where .NET framework is not installed but the assemblies are in there, it fails saying that it cannot find type System.Runtime.Serialization.Json.DataContractJsonSerializer. I have code which loaded the System.Runtime.Serialization assembly.

  • #27029
    Profile photo of Don Jones
    Don Jones

    PowerShell doesn't run without .NET. PowerShell itself is coded in .NET.

    The "Program Files" location isn't .NET. It's usually supporting files and reference assemblies. It shouldn't be necessary. "Programs and Features" also doesn't necessarily list every version of .NET, because not all of them are optional. Non-optional versions aren't usually listed.

    Your problem is more likely that the machine has only an OLDER version of .NET – enough to run PowerShell, but without the JSON serializer stuff from a newer version.

  • #27030
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    You're right .NET Framework is installed. And it's the 4.5 version, which is the same as what I have in the other environment where the script is working. Now I'm not sure what's causing the script to fail in this environment.

  • #27032
    Profile photo of Tim Pringle
    Tim Pringle

    Hey Kahlan,

    If you're able to post the code for us to have a look, maybe we can help? We've been known to look at PowerShell problems on this forum now and again.... 😉

  • #27042
    Profile photo of Richard Siddaway
    Richard Siddaway

    Your problem is that System.Runtime.Serialization.Json.DataContractJsonSerializer is in the System.Runtime.Serialization .NET namespace which is NOT loaded by default into PowerShell.

    Try using Add-Type to load the assembly containing System.Runtime.Serialization.Json.DataContractJsonSerializer

    Add-Type -AssemblyName System.Runtime.Serialization

    You can then create a serializer

    $x = [System.Runtime.Serialization.Json.DataContractJsonSerializer]

  • #27080
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    I did load the assembly.


    I verified it was loaded by writing this $Message variable to a file. I also outputted some other things:

    $Message += "`r`n" + ([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -eq "System.Runtime.Serialization" }).FullName
    $Message += "`r`nPowerShell Version: " + $PSVersionTable.PSVersion.ToString()
    $Message += "`r`n.NET Version: " + [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory().ToString()
    $Message += "`r`n" + ([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -eq "System.Runtime.Serialization" }).FullName
    $platform = [IntPtr]::size -eq 8
    $Message += "`r`nIs 64-bit? " + $platform.ToString()

    Here's the output of the file:

    PowerShell Version: 2.0
    .NET Version: C:\Windows\Microsoft.NET\Framework\v2.0.50727\
    System.Runtime.Serialization, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Is 64-bit? False

    I did this for both environment and the output file is the same. It's just in the other environment, it's giving me the error that i can't find datacontractjsonserializer.

You must be logged in to reply to this topic.