Powershell 3.0 Support

Aug 28, 2012 at 4:22 PM

Does SQLPSX support PowerShell 3.0? 

My $PSVersionTable reports the following: 

WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0}
SerializationVersion           1.1.0.1
BuildVersion                   6.2.8370.0
PSVersion                      3.0
CLRVersion                     4.0.30319.269
PSRemotingProtocolVersion      2.2

But when I try to do an Import-Module SQLPSX for the SQLISE module I get the following errors:

Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:6 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException 

You cannot call a method on a null-valued expression.At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:8 char:9+     if ($userStore.GetDirectoryNames($dirName))+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull 

Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:38 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException 

You cannot call a method on a null-valued expression.At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:40 char:9+     if (-not $userStore.GetDirectoryNames($dirName))+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull 

You cannot call a method on a null-valued expression.At

C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:65 char:9+         $file.Close()+         ~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull 

You cannot call a method on a null-valued expression.At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:66 char:9+         $userStore.Close()+         ~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull 

Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:6 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException 

Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\Library-UserStore.ps1:77 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException 

Read-UserStore : Cannot open file SQLIse\options.txtAt C:\Users\john\Documents\WindowsPowerShell\Modules\SQLISE\SQLISE.psm1:43 char:12+ $options = Read-UserStore -fileName "options.txt" -dirName "SQLIse" -typeName "H ...+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Read-UserStore

Coordinator
Aug 28, 2012 at 8:06 PM

See related discussion http://sqlpsx.codeplex.com/discussions/277058

Aug 29, 2012 at 1:56 PM

Sorry - what am I missing?  the link you provided seems to be dead.  Or at least I can't see that discussion.  

I did try to search for similar postings prior to posting mine, but didn't find anything along the same lines. 

If you could let me know what was discussed in 277058 I would appreciate it. 

Coordinator
Aug 29, 2012 at 6:44 PM

My bad. I just noticed it was a discussion marked private by one of the project team members. I don't see any reason why it should private, but unfortunately there isn't a way to change discussion from private to public so here's a copy  of the thread I linked to. The fix is in the source code area, but hasn't been bundled into a release. The issue deals with the DLR implementation of Powershell in V3. There's also issues with WPK (the WPF Powershell framework we used in V2) and ISE 3.0 object model changes in V3 which hasn't been fixed because it involves re-writing the ISE module to use C#/WPF with the new ISE object model.


I'm getting a lot of errors (from SQLIse) starting ISE with the 3.0 CTP1 installed.  Is this just a problem with my install or is this what everyone is seeing?
Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:6 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException You cannot call a method on a null-valued expression.At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:8 char:9+     if ($userStore.GetDirectoryNames($dirName))+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:38 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException You cannot call a method on a null-valued expression.At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:40 char:9+     if (-not $userStore.GetDirectoryNames($dirName))+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression.At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:65 char:9+         $file.Close()+         ~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression.At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:66 char:9+         $userStore.Close()+         ~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:6 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException Exception calling "GetUserStoreForAssembly" with "0" argument(s): "Unable to determine granted permission for assembly."At C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\Library-UserStore.ps1:77 char:5+     $userStore = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreFo ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : IsolatedStorageException Read-UserStore : Cannot open file SQLIse\options.txtAt C:\Users\Mike\Documents\WindowsPowerShell\Modules\SQLIse\SQLIse.psm1:43 char:12+ $options = Read-UserStore -fileName "options.txt" -dirName "SQLIse" -typeName "H ...+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Read-UserStore

 

 

*************************

Here's the simplest code to repo:

[System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreForAssembly()�

****************************

I posted a fix in source code area which will work in V2 and V3. Only one file is updated the Library-UserStore.ps1 which is part of the SQLIse module. Joel Bennett provided the workaround. This is one of the breaking change in V3. Here's the explanation Joel provided...
Basically, it goes something like this ...
    // for requests that want local instead of roaming ...

    if (!IsolatedStorage.IsRoaming(scope))    {        // get the permissions granted to the calling assembly         RuntimeAssembly caller = IsolatedStorage.GetCaller();        IsolatedStorage.GetControlEvidencePermission().Assert();        caller.GetGrantSet(ref permissionSet, ref permissionSet2);                // and if they have none, die        if (permissionSet == null)            throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_AssemblyGrantSet"));    }Basically, the root cause is that you've gone from calling from a GAC'd assembly with a StrongName and a security policy, to calling from an anonymously hosted DynamicMethods assembly.  Try this in PowerShell -Version 2.0 (in 3.0 CTP you get this):
PS> [System.Reflection.Assembly]::GetExecutingAssembly() |fl f*,L*,M*,G*,Ev*
FullName            : Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=nullLocation            :ManifestModule      : RefEmit_InMemoryManifestModuleGlobalAssemblyCache : FalseEvidence            : {<System.Security.Policy.Url version="1">                      <Url>file:///C:/Windows/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll</Url>                      </System.Security.Policy.Url>                      , <System.Security.Policy.Zone version="1">                      <Zone>MyComputer</Zone>                      </System.Security.Policy.Zone>                      }
In point of fact:
PS3> [System.Reflection.Assembly]::GetExecutingAssembly().GetType().FullNameSystem.Reflection.Emit.InternalAssemblyBuilder
PS2> [System.Reflection.Assembly]::GetExecutingAssembly().GetType().FullNameSystem.Reflection.Assembly
As a work around, I think you need to call that from inside a Add-Type assembly something like this...
Add-Type -Namespace SQLPS -Name IsolatedStorage -MemberDefinition @"public static System.IO.IsolatedStorage.IsolatedStorageFile GetUserStoreForAssembly() {   return System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForAssembly();}"@$store = [SQLPS.IsolatedStorage]::GetUserStoreForAssembly()

Unlike the Reflection.Emit apis, the DLR doesn’t provide a way to specify the hosting assembly.
*****************************

One other thing, WPK does not work under Powershell V3. ShowUI works but still has some issues in V3 and Bernd mentioned it doesn't cover all of our use cases. So, SQLIse is still going to be broken in V3 to an extent. Although you can still connect to a database and execute a query. I think we'll need to wait until ShowUI is compatible with V3 and rework SQLIse at that time.