Author Posts

July 1, 2015 at 6:28 pm

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.

July 1, 2015 at 6:41 pm

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.

July 1, 2015 at 7:18 pm

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.

July 1, 2015 at 11:03 pm

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.... 😉

July 2, 2015 at 2:45 am

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]

July 2, 2015 at 10:14 am

I did load the assembly.

[System.Reflection.Assembly]::LoadWithPartialName("System.Runtime.Serialization")

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=3.0.0.0, 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.