Unable to access the id based on the GUID inside my hash table inside my power shell script

john john 1,021 Reputation points
2023-07-15T03:10:09.1333333+00:00

I have this code to build a hash table with GUID and ID, and then i am trying to get the ID based on the GUID, but the ID (fileID variable inside my code) will always be null:-

$itemHashTable = @{}
$fileItems = Get-PnPListItem -List $listName -PageSize 5000


ForEach($item in $fileItems)
{
$itemId = $item["ID"]
$itemGuid =  $item["GUID"]  

# Add the ID and GUID to the hash table
$itemHashTable.add($itemGuid,$itemId)
$itemHashTable[$itemGuid]
}
$itemHashTable

$customerFolderFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $serverRelativeUrl -Recursive

ForEach($customerFolderFile in $customerFolderFiles)
{
$guid = $customerFolderFile.UniqueId 

$fileId = $itemHashTable[$guid] ### this will always be null

any advice? Thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-07-17T13:30:23.84+00:00
    Hello John,
    
    Thank you for your question and for reaching out with your question today.
    
    Based on the code you provided, it seems that the issue lies in accessing the ID based on the GUID inside your hash table. Here are a few suggestions to help you resolve the problem:
    
    1. GUID Comparison: Ensure that you are comparing the GUIDs correctly. Sometimes GUID values might have additional formatting or different representations that could prevent a direct match. Try converting both the GUID in the hash table and the GUID from `$customerFolderFile.UniqueId` to a common format (e.g., without dashes) before comparing them.
    
    2. Hash Table Key Case Sensitivity: Check if the GUIDs in your hash table keys are case-sensitive. In some cases, hash table keys can be case-sensitive, so make sure that the letter casing of the GUIDs is consistent when accessing the hash table.
    
    3. GUID Data Type: Confirm that the GUID data type is consistent between the hash table and the `$customerFolderFile.UniqueId` value. If one is stored as a string and the other as a different data type (e.g., `System.Guid`), the comparison may fail. Ensure that both are of the same data type for accurate matching.
    
    4. Hash Table Population: Verify that the hash table is populated correctly before attempting to access the values. You can check the hash table's contents by outputting the hash table using `Write-Output $itemHashTable` or using `Get-Member` to examine the hash table's structure and data.
    
    5. Debugging: Add some additional debugging statements to your code to investigate the issue further. For example, you can output the GUID and ID values during the population of the hash table and compare them with the values in the `$customerFolderFiles` loop to identify any inconsistencies or differences.
    
    6. Use `ContainsKey()`: Instead of directly accessing the hash table using `$itemHashTable[$guid]`, you can use the `ContainsKey()` method to check if the hash table contains a specific key before accessing its value. This can help ensure that the key exists before attempting to retrieve the corresponding ID.
    
    Here's an example:
    
    ```powershell
    if ($itemHashTable.ContainsKey($guid)) {
        $fileId = $itemHashTable[$guid]
    } else {
        # Handle case when the GUID is not found in the hash table
        Write-Output "GUID not found: $guid"
    }
    

    By implementing these suggestions, you should be able to troubleshoot and resolve the issue of accessing the ID based on the GUID inside your hash table.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Best regards.

    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.