Windows RT Powershell (PermissionDenied) on New-Object - powershell

I am trying to instantiate an object in Powershell for Windows RT, but keep getting the following error.
PS > $foo = New-Object System.Security.Cryptography.SHA1Managed
New-Object : Cannot create type. Only core types are supported in this language mode.
At line:1 char:8
+ $foo = New-Object System.Security.Cryptography.SHA1Managed
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Object], PSNotSupportedException
+ FullyQualifiedErrorId : CannotCreateTypeConstrainedLanguage,Microsoft.PowerShell.Commands.NewObjectCommand
I just spent the last thirty minutes engaged in some pretty heavy Google-fu and was unable to find anything even close to a similar problem, let alone an answer. My hope is that I just need to configure something; my fear is that Windows RT ships with a crippled version of Powershell.
Does anyone know which case it is?

Yup, Windows RT ships with a locked down version of PowerShell - it operates in ConstrainedLanguageMode which prevents most interaction (prop setters/method calls) with .NET. It also has a limited command set IIRC.

Related

InvalidCastException when trying to obtain UserPrincipal.Current

I have a PowerShell script which checks the currently signed in user as part of its start-up process. I'm using .Net to do this by adding the assembly:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$cUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
For almost everyone this works fine and I get a UserPrincipal object that I can use elsewhere, however there are a couple of users who get the following error when running it:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. At line:2 char:1
+ [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException
This is on a standard Windows 10 20H2 install and doesn't matter if PowerShell is running elevated or not. I've never seen this call return anything other than a UserPrincipal before, so I would be grateful for any advice: is this something I can deal with in code, or is there some underlying issue with the machines that are returning this exception?
I don't know if this is what you're looking for:
$cUser = Get-ADUser $Env:Username -Properties *
This has the most important attributes that you can use for whatever you want later on. It provides as much attribute as the code you posted that didn't work for some of the user.

Error downloading files with DownloadFile in powershell 2

I have the need to download files under powershell 2.
Since Invoke-WebRequest is not supported in version 2, I try to use DownloadFile but I always get the same error and I don't know how to fix it.
PS C:\Users\user> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
:: : Exception setting "SecurityProtocol": "Cannot convert null to type "System.Net.SecurityProtocolType" due to invali
d enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values ar
e "Ssl3, Tls"."
At line:1 char:28
+ [Net.ServicePointManager]:: <<<< SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
PS C:\Users\user> $client = new-object System.Net.WebClient
PS C:\Users\user> $client.DownloadFile("https://web.com/test.txt", "C:\Users\user\Desktop\test.txt")
DownloadFile : Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpec
ted error occurred on a send."
At line:1 char:21
+ $client.DownloadFile <<<< ("https://web.com/test.txt", "C:\Users\user\Desktop\test.txt")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
What I can do?
If you are getting an error setting the TLS version, you may want to check which protocols are available:
[Net.SecurityProtocolType]::GetValues( 'Net.SecurityProtocolType' )
However, I don't know how the protocol availability will be affected by using such an old version. Moreover, I don't know how specific protocol settings on a given system affect the availability of protocols either at large or as returned by the above command.
In fact, I would point out the error itself only reflects SSL3 & Tls. On my system when I enter an invalid enumerator the error reports Ssl3,Tls,Tls11,Tls12,Tls13 as choices. While my system is configured to disable everything below Tls12 and has no settings defined for Tls13.
The best assumption I can make is .Net 2.0 which is used with PowerShell 2.0 cannot use Tls12 as it's not present in the enumerators and based on comparison with my system the enums aren't affected by explicit enabling/disabling of protocols.
In any case, I think you have to clear up the first error before you worry about the second. The second error may well be due to the TLS incompatibility. The easiest way to do this would be to upgrade PowerShell (along with the required .Net version). PowerShell 2.0 can coexist with Windows PowerShell 5.1 though you may have to launch with the -Version 2.0 parameter. Also, make sure the engine is installed as a Windows feature. DISCLAIMER: verify all that on your own.
I know the Windows PowerShell 2.0 Engine (PowerShell-V2) was available as a feature at least as late as Windows 2012 R2.
I would also point out that PowerShell 7.x (aka PowerShell Core) can be installed side by side with Windows PowerShell. Potentially this could sidestep the aforementioned complications, allowing you to do run the task at hand...

Issue with QR Decoding using Zxing.Net in Powershell - Cannot find an overload for "Decode" and the argument count: "1"

I am trying to decode an QR Code in PowerShell with using Zxing.net (https://github.com/micjahn/ZXing.Net)
There is a HowTo at this page but I am not able to use it that way:
https://github.com/micjahn/ZXing.Net/wiki/Using-ZXing.Net-with-Powershell
Everytime I run the script I receive the following error message:
Cannot find an overload for "Decode" and the argument count: "1".
At C:\Users\ww\Desktop\reader.ps1:13 char:1
+ $result = $reader.Decode($bitmap)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
My whole script is:
Add-Type -Path "C:\Users\ww\Desktop\zxing.net\lib\net45\zxing.dll"
$reader = New-Object -TypeName ZXing.BarcodeReader
$reader.Options.TryHarder=1
# set TryHarder option to true, other options can be set the same way
$bitmap = [System.Drawing.Bitmap]::FromFile("C:\Users\ww\Desktop\abc.bmp")
$result = $reader.Decode($bitmap)
$bitmap.Dispose()
$result.Text
I only know "overload" from Java, where I can use different parameters for the same function, but in this case it makes no sense to me (I am not a very experienced programmer).
Is it possible that I am using the wrong .dll? I downloaded the Nuget-Package from https://www.nuget.org/packages/ZXing.Net/0.16.5, then extracted it via 7-Zip. I have tried various .dll (net40, netcoreapp3.0, portable, net20,...) but the result was always the same.
I also found this Github thread, where it seems rather easy to get everything working.
Could anyone give me a hint what is wrong with my implementation?
Thank you!
The script as-is should work fine. Make sure you are using the .dll for the appropriate version of .NET installed on your system. For example, I have .NET version 4.8, so I used the .dll for the highest version available, 4.7. My file-path is C:\...\ZXing.Net.0.16.5.0\net4.7\zxing.dll .
Add-Type -Path "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Zxing\zxing.dll"
$reader = New-Object -TypeName ZXing.BarcodeReader
$reader.Options.TryHarder = 1 # set TryHarder option to true, other options can be set the same way
$bitmap = [System.Drawing.Bitmap]::FromFile("C:\Users\testUser\Pictures\QR.png")
$result = $reader.Decode($bitmap)
$bitmap.Dispose()
$result.Text
Console Output: https://www.youtube.com/watch?v=oHg5SJYRHA0

Word as COM object has no attributes/ability to set attributes

I'm sorry that this is quite alien to me, but I had a previously working script that functioned like the following:
$wd = new-object -comobject word.application
but when I print the COM object it has empty/blank values for all attributes.
PS C:\Users\XXX> $wd
Application :
Creator :
Parent :
Name :
Documents :
Windows :
ActiveDocument :
ActiveWindow :
Selection :
And it cannot set any or perform any functions
PS C:\Users\XXX> $wd.Visible = $true
Exception setting "Visible": "Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This
operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: Error loading
type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))."
At line:1 char:1
+ $wd.Visible = $true
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
I tried browsing related questions but was unable to see if the mistakes are exactly identical. I've run all Windows updates and did a repair installation of Office. (Running Windows 10 and Office 2013)
Sometimes Word/Excel/PPT COM Objects are acting wildly. Thus using these is not recommended for automation purposes by Microsoft itself (sorry but couldn't find the source). But whatever personally I use those all the time :).
System Restore or closing and repoening the Powershell Session actually fixes those problems for me.
Unfortunately I couldn't figured out a programmatic way to handle this yet. So looking forward for further answers. (+1)

PowerShell and Onenote

I would like to read and write on OneNote pages using PowerShell scripts. I have been trying different scripts I found, but none of them even run on my machine. I am wondering if I have to enable something, or get some kind of library.
When trying to run some scripts I found, I get this error:
Unable to find type [Microsoft.Office.InterOp.OneNote.HierarchyScope].
At line:3 char:27
+ ... erarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPage ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Offic....HierarchyScope:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
Could someone point me in the right direction? Or is it even possible?
PS Version: 5.1.17134.407
OneNote 2013
Agreed with Theo on this one.
How are you trying to run them, consolehost, ISE, VSCode, other editor?
Did you set your ExecutionPolicy so that you could run them at all.
Is OneNote on your machine and activated or are you trying to hit a remote box?
However, there is a provider you can use to assist you here...
OneNotePowerShellProvider
# Example Scripts
Name
----
ConvertTo-Object.ps1
Create-OneNoteDocumentation.ps1
Export-PsOn.ps1
Get-OneNoteApplication.ps1
Get-OneNoteDigest.ps1
Get-OneNoteText.ps1
Get-ProviderTests.ps1
Import-FilesToOneNote.ps1
Robocopy-Items.ps1
Set-OneNoteDebug.ps1
Start-Tests.ps1
… as well as this article...
Read and Write Content from OneNote with PowerShell
# Examples
# get a table of all notebooks
$OneNote = New-Object -ComObject OneNote.Application
[xml]$Hierarchy = ""
$OneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy)
$Hierarchy.Notebooks.Notebook | Format-Table Name, path, isUnread, isCurrentlyViewed