Closed Obenscripe closed 1 year ago
Thank you for your feedback. This has been routed to the support team for assistance.
@Obenscripe Thanks for reaching out to us and reporting this issue. We are looking into this issue and we will provide an update.
@Obenscripe I have installed the same2.11.1
version of the Az.Accounts module. But I am unable to reproduce this issue at my end. Could you please try the below steps and let me know if that helps ?
Well I never - I've been having issues with getting a correct and successful AZ connection for a week and this may have sorted it. What I don't understand is that I installed the 2.11.1 module in the first place using Install-module Az.Accounts -allowclobber -Force but it was still giving the error above. Trying again seems to have worked (within this session) - although in fairness i have restarted the box twice this afternoon in order to fix an unrelated issue with SMB. but the restart have have helped. PowerShell_transcript.HOR-UDS-ADM-V01.k9oFCTDV.20230118172914.txt
I'll continue to test with unattended connections as that was where my initial issue was (with version 2.10.4) Why would the -allowclobber -Force not have worked where as re doingt he process today using just -AllowClobber seems on the face of it, to have worked? Thanks Dan
Nope - Ignore me. Whilst that comment above says it worked - and it did. once I closed the ISE then re opened ISE in an elevated session - I always run in an elevated session. I could not repeat the success of just before.
Tihis is the sceen shot showing the error and the dll file failing to be loaded but it does exist.
And this is weird. closing and window and opening a new elevated ISE I can run it again and now it tells me the module is not installed despite the fact it's installed and imported...
I'm out of ideas now.
Hi @Obenscripe , first of all, the "module is not installed" message is kind of misleading here. What happened was that the module was installed but failed to be imported. We'll definitely improve the message here.
I doubt there was another module intefering with the import of Az.Accounts. Because we are loading a higher version of Microsoft.Identity.Client.Extensions.Msal.dll than 2.18.4.0. Could you do Get-Module
in a new ISE session and share the output?
Also, Resolve-Error
isn't going to work because it's inside Az.Accounts. To get more info about the error, let's reproduce the issue by Import-Module Az.Accounts
and check the $Error
object. Usually we do
$error[0]
$error[0].Exception
$error[0].Exception | Format-List * -Force
$error[0].Exception.InnerException
Hi Isra-fel This is how my code works:
I'm importing the connection code using a module and storing settings in the registry:
function ConnectTo-AzAccount_RSA.GSO.Graph.Access.Reporting{
[CmdletBinding()]
param(
)
$MSGregCreds = Get-ItemProperty -Path HKCU:\Software\ScheduledScripts\MicrosoftGraph_OAuth2
$MSGSecurePassword = $MSGregCreds.Thumbprint | ConvertTo-SecureString
$MSGrawUsername = $MSGregCreds.AppId
$MSGcreds = New-Object System.Management.Automation.PSCredential -ArgumentList $MSGrawUsername, $MSGSecurePassword
$AppId = $MSGcreds.UserName
$Thumb = $MSGcreds.GetNetworkCredential().Password
[string] $TenantId = "4dc567e4-2b82-4a00-bcdb-f1f6782a0f6e"
#Write-host "ThumbP = $Thumb"
#Write-host "AppId = $AppId"
#Write-host "TenantId = $TenantId"
$AZsession = Connect-AzAccount -CertificateThumbprint $Thumb -ApplicationId $AppId -Tenant $TenantId -ServicePrincipal
return $AZsession
}
and I'm importing that and other modules at the top of my code.
# Sharepoint
Import-Module PnP.PowerShell
### logging functions module ###
Import-Module D:\PowerShell\ScheduledScripts\Utilities\Logging\LoggingFunctions.vProd.psm1
# connections to 365
Import-Module "D:\PowerShell\ScheduledScripts\Utilities\Connect\ConnectToOffice365.vProd.psm1"
# Azure AD Log analytics
import-module Az.OperationalInsights
import-module Az.Accounts
Then calling the connection function
### New Connection
$AZsession = ConnectTo-AzAccount_RSA.GSO.Graph.Access.Reporting
From a new elevated ISE window this morning the issue does not immediately show up:
PS C:\Windows\system32> import-module az.accounts
PS C:\Windows\system32> $Error
PS C:\Windows\system32> $error[0]
$error[0].Exception
$error[0].Exception | Format-List * -Force
$error[0].Exception.InnerException
PS C:\Windows\system32> get-module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 2.11.1 az.accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault...}
Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}
PS C:\Windows\system32>
Hmmm, it seems I can't reproduce the issues this morning either. Which is odd because I did reproduce the error several times last night and each time using a new clean elevated ISE window, as I'm doing this morning. This is why this particular issue is frustrating. because It seems fine now but when I trigger my script later from a scheduled task running full unattended (right now I'm running the full script from the file in ISE) It will likely continue to fail. I have to let this run for now but will report back with further testing... Thanks Dan
And sure enough - running as a scheduled task under the same Id As I am logged in with, with highest privilege.. it fails I have attached the two transcript files.
20230119-091604-InvokeKQL.vProd.transcript.log 20230119-091041-InvokeKQL.vProd.transcript.log
As you say, however, we appear to have worked past the first error when trying to import the module. But that still seems to be failing as the Connect command isn't found. the later file has the full exception object. Not sure where to go next? Thanks
Hey @Obenscripe
Pretty sure there was a conflict between PnP.PowerShell and Az.Accounts, because I can now reproduce by Import-Module PnP.PowerShell; Import-Module Az.Accounts
.
I need some time to figure out what exactly caused the conflict, but one thing worth trying is to import Az.Accounts before PnP.PowerShell. I wasn't able to reproduce after changing the order.
isra-fel youre a Hero! And you've saved my week!
I swapped the order of the import lines at the top of the script and it's running about 10 minutes already without error. I'll keep an eye for this in other scripts we have as we will be using Az connections more and more. This also explains when I managed to test successfully but then failed when running the prod script unattended because the prod script order was pnp.powershell first. I didn't import the pnp module in all of my testing because that wasn't where the issue appeared to be.
Your're workaround is a complete success. Thanks for this, really appreciate your help. I'll keep an eye on future versions, but with this workaround I can continue. (without thinking I'm going nuts or missing something :-))
Oops, hold on...
The prod script ran successfully until near the end it came up with this error: not seen one like this before when running powershell... The process cannot access the file 'C:\Windows\system32\1' because it is being used by another process.
Message : The process cannot access the file 'C:\Windows\system32\1' because it is being used by another process. Data : {} InnerException : TargetSite : Void WinIOError(Int32, System.String) StackTrace : at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Management.Automation.PathUtils.MasterStreamOpen(PSCmdlet cmdlet, String filePath, Encoding resolvedEncoding, Boolean defaultEncoding, Boolean Append, Boolean Force, Boolean NoClobber, FileStream& fileStream, StreamWriter& streamWriter, FileInfo& readOnlyFileInfo, Boolean isLiteralPath) HelpLink : Source : mscorlib HResult : -2147024864
Transcript stopped, output file is D:\PowerShell\ScheduledScripts\UpdateGSOCertsSecretsAndAdminSite\Log\20230120-060411-InvokeKQL.vProd.transcript.log
PS C:\Windows\system32>
Got it. I had a panic for a moment there. The scheduled task was still enabled and it over ran my test run in the ISE and clashed writing the log file. Testing again now...
Completeted without issue.! Phew! Thanks isra-fel, it's all good. Cheers Dan
@Obenscripe Thanks for getting back. We will now proceed with closure of this GitHub issue. If you need any further assistance on this issue in future, please feel free to reopen this thread. We would be happy to help.
Description
This is module version 2.11.1 (which is the hotfix for 2.10.4.) Either I am missing something or this hotfix is not fully working on my environment. PS 5.1, server 2016 running in an elevated ISE window.
Why does the "resolve-azerror" say "Not Installed?" I just un-installed then re installed the latest version 2.11.1 hotfix so this is a bit odd. is this because the module cannot be imported?
Issue script & Debug output
Environment data
Module versions
Error output