Add-type compile errors when running script as SYSTEM

When I attempt to run the following snippet of code as SYSTEM

add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificates;public class TrustAllCertsPolicy : ICertificatePolicy {public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem) {return true;}}";$AllProtocols = [System.Net.SecurityProtocolType]"Ssl3,Tls,Tls11,Tls12";[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols;[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

I get the following errors:

add-type : c:\Windows\Temp\1xdyb5zc\1xdyb5zc.0.cs(1) : A namespace cannot directly contain members
such as fields or methods
c:\Windows\Temp\1xdyb5zc\1xdyb5zc.0.cs(1) : >>> PSusing System.Net;PSusing
System.Security.Cryptography.X509Certificates;public class TrustAllCertsPolicy : ICertificatePolicy
{public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest
request, int certificateProblem) {return true;}}
At line:1 char:1
+ add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (c:\Windows\Temp...elds or methods:CompilerError) [Add-T
ype], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

add-type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

New-Object : Cannot find type [TrustAllCertsPolicy]: verify that the assembly containing this type
is loaded.
At line:1 char:478
+ ... ficatePolicy = New-Object TrustAllCertsPolicy
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Is there something about running this chunk of code as SYSTEM which is not allowing it to run. It runs properly when running as a normal user.

What is PSusing ?

You can read the examples from below documentation for Add-Type cmdlet.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-5.1

PSUsing was something I saw I needed to change to because I was getting errors using well using.

I made the change and it seems to have worked. Thank you.