title: Powershell script fails during remote execution but works locally
body: <p>I am attempting to execute the code below. The errors are self-explanatory but only occur when run remotely. When run locally, all the code works and I can verify that they have the desired effect by pulling content from a web server that requires Tls12 and receiving an error when I have not changed the security protocol.</p>
  
  <pre><code>$tls12 = [Enum]::ToObject([Net.SecurityProtocolType], 3072)
  [Net.ServicePointManager]::SecurityProtocol = $tls12
  </code></pre>
  
  <p>When run on the server, they execute flawlessly. When run remotely via Invoke-Command I receive this error.</p>
  
  <pre><code>Exception setting "SecurityProtocol": "The requested security protocol
  is  not supported."
  + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : PropertyAssignmentException
  + PSComputerName        : servername
  </code></pre>
  
  <p>Alternatively, this line of code which is fundamentally the same.</p>
  
  <pre><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  </code></pre>
  
  <p>This also works when run on the server directly but results in this error when run remotely.</p>
  
  <pre><code>Exception setting "SecurityProtocol": "Cannot convert null to type
   "System.Net.SecurityProtocolType" due to invalid enumeration values. Specify one of the following enumeration values and 
  try again. The possible enumeration values are "Ssl3, Tls"."
  + CategoryInfo : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : PropertyAssignmentException
  + PSComputerName : servername
  </code></pre>
  
  <p>The remote server Windows Server 2008 R2 and is running powershell 2.0. The latest Framework version installed is 4.5 release 378389. The machine I am executing from is Windows 10 running powershell 5.0 and Framework 4.6.2 in case it matters.</p>
  
  <p>This is the code I am using to execute it remotely.</p>
  
  <pre><code>$webSession = New-PsSession -ComputerName servername
  
  $cmd = {
      #$tls12 = [Enum]::ToObject([Net.SecurityProtocolType], 3072)
      #[Net.ServicePointManager]::SecurityProtocol = $tls12
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  }
  
  Invoke-Command -ScriptBlock $cmd -Session $webSession
  remove-pssession  $webSession
  </code></pre>
  
  <p>If anyone has an idea or suggestion, I would greatly appreciate the help.</p>