Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

Mit dem Absenden dieses Formulars akzeptiere ich die Datenschutzerklärung von NinjaOne.

Wie man mit PowerShell auf Windows-Benutzerprofilgrößen zugreift und sie speichert

PowerShell hat zweifellos die Art und Weise revolutioniert, wie IT-Profis Windows-Systeme verwalten, alltägliche Aufgaben automatisieren und tiefer in die Systemdiagnose eintauchen. Heute befassen wir uns mit einem PowerShell-Skript, das zur Bewertung der Größe von Benutzerprofilen unter Windows entwickelt wurde und ein wichtiges Tool für Systemadministratoren und Managed Service Provider (MSPs) darstellt.

Hintergrund

Mit der Zunahme von Big Data und immer anspruchsvolleren Anwendungen sind die Verwaltung von Speicherplatz und die Gewährleistung einer effizienten Festplattennutzung von größter Bedeutung. IT-Fachleute und MSPs haben oft mit Problemen zu kämpfen, die sich aus der Ansammlung großer Datenmengen durch die Benutzer ergeben, manchmal ohne es zu wissen. Dieses Skript löst dieses Problem, indem es IT-Experten ermöglicht, die Größe von Benutzerprofilen schnell zu bestimmen und so eine optimale Speicherverwaltung und Systemintegrität zu gewährleisten.

Das Skript

#Requires -Version 5.1

<#
.SYNOPSIS
    Updates a Custom Field with the total size of all User Profiles.
    If the Max parameter is specified then it will return an exit code of 1
     for any profile being over that Max threshold in GB.
.DESCRIPTION
    Updates a Custom Field with the total size of all User Profiles.
    If the Max parameter is specified then it will return an exit code of 1
     for any profile being over that Max threshold in GB.
.EXAMPLE
     -Max 60
    Returns and exit code of 1 if any profile is over 60GB
.EXAMPLE
     -CustomField "Something"
    Specifies the name of the custom field to update.
.EXAMPLE
    No Parameter needed.
    Uses the default custom field name: TotalUsersProfileSize
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [Alias("MaxSize", "Size", "ms", "m", "s")]
    [Double]
    $Max,
    [Parameter()]
    [Alias("Custom", "Field", "cf", "c", "f")]
    [String]
    $CustomField = "TotalUsersProfileSize"
)

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
    function Format-FileSize {
        param($Length)
        switch ($Length) {
            { $_ / 1TB -gt 1 } { "$([Math]::Round(($_ / 1TB),2)) TB"; break }
            { $_ / 1GB -gt 1 } { "$([Math]::Round(($_ / 1GB),2)) GB"; break }
            { $_ / 1MB -gt 1 } { "$([Math]::Round(($_ / 1MB),2)) MB"; break }
            { $_ / 1KB -gt 1 } { "$([Math]::Round(($_ / 1KB),2)) KB"; break }
            Default { "$_ Bytes" }
        }
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }

    $Profiles = Get-ChildItem -Path "C:Users"
    $ProfileSizes = $Profiles | ForEach-Object {
        [PSCustomObject]@{
            Name   = $_.BaseName
            Length = Get-ChildItem -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Property Sum -ExpandProperty Sum
        }
    }
    $Largest = $ProfileSizes | Sort-Object -Property Length -Descending | Select-Object -First 1

    $Size = $ProfileSizes | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Property Sum -ExpandProperty Sum

    $FormattedSize = Format-FileSize -Length $Size

    $AllProfiles = $ProfileSizes | Sort-Object -Property Length -Descending | ForEach-Object {
        $FormattedSizeUser = Format-FileSize -Length $_.Length
        "$($_.Name) $($FormattedSizeUser)"
    }

    Write-Host "All Profiles - $FormattedSize, $($AllProfiles -join ', ')"

    Ninja-Property-Set -Name $CustomField -Value "$AllProfiles"

    if ($Max -and $Max -gt 0) {
        if ($Largest.Length -gt $Max * 1GB) {
            Write-Host "Found profile over the max size of $Max GB."
            Write-Host "$($Largest.Name) profile is $($Largest.Length / 1GB) GB"
            exit 1
        }
    }
    exit 0
}
end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

Das betreffende Skript ist in folgende Abschnitte unterteilt:

  • Cmdlet-Bindung und Parameter: Das Skript beginnt mit der Definition von Parametern, z. B. Max, das einen optionalen Schwellenwert für die Profilgröße festlegt, und CustomField, das den Namen des zu aktualisierenden benutzerdefinierten Feldes angibt.
  • Abschnitt beginnen: Hier haben wir zwei Hilfsfunktionen:
    • Test-IsElevated: Prüft, ob das Skript mit Administratorrechten ausgeführt wird.
    • Format-FileSize: Konvertiert Bytes in ein lesbares Dateigrößenformat.
  • Prozessabschnitt:
    • Das Skript prüft, ob es über Administratorrechte verfügt.
    • Es liest dann alle Benutzerprofile aus „C:Users“ aus und berechnet deren Größe.
    • Die Profile werden sortiert und die Gesamtgröße wird berechnet.
    • Die Daten werden an den Host ausgegeben, und die Funktion „Ninja-Property-Set“ aktualisiert das benutzerdefinierte Feld.
    • Wenn ein Max-Parameter angegeben ist, prüft das Skript auf Profile, die diese Größe überschreiten, und beendet sich mit einem entsprechenden Code.

Potenzielle Anwendungsfälle

Stellen Sie sich einen MSP vor, der den Speicherplatz für einen Firmenkunden beaufsichtigt. Das Unternehmen sieht sich plötzlich mit Speicherplatzproblemen konfrontiert. Anstatt sich für teure Speicher-Upgrades zu entscheiden, verwendet der MSP dieses Skript, um übergroße Benutzerprofile zu identifizieren. Anhand dieser Daten können sie sich dann mit bestimmten Benutzern oder Abteilungen in Verbindung setzen, um die Speicherung besser zu verwalten und zu optimieren.

Vergleich

Um die kumulativen Größen von Windows-Benutzerprofilen zu ermitteln, war bisher eine manuelle Navigation durch Eigenschaften, Anwendungen von Drittanbietern oder umständliche Skripte erforderlich. Dieses PowerShell-Skript bietet einen optimierten Ansatz, bei dem native Windows-Funktionen verwendet werden, um präzise und umsetzbare Erkenntnisse zu liefern.

FAQs

  • Sind Administrator-Rechte zwingend notwendig?
    Ja, das Skript erfordert zur Ausführung Administrator-Rechte.
  • Was passiert, wenn ein Benutzerprofil die angegebene maximale Größe überschreitet?
    Das Skript gibt eine Meldung aus, die angibt, welches Profil die Größe überschreitet, und gibt einen Exit-Code von 1 zurück.
  • Wie lautet der Standardname des benutzerdefinierten Feldes, wenn er nicht angegeben wird?
    Er lautet „TotalUsersProfileSize“.

Auswirkungen

Dieses Skript ist zwar von großem Nutzen, aber wenn es nicht richtig eingesetzt wird, kann es zu Problemen mit dem Datenschutz führen. Die Extraktion und mögliche Weitergabe von Benutzerprofilgrößen könnte von einigen Nutzern als invasiv empfunden werden. Für die IT-Sicherheit kann das Verständnis der Speichergewohnheiten der Benutzer außerdem eine proaktive Möglichkeit sein, ungewöhnliche Datenspitzen zu erkennen, die auf Malware oder eine mögliche Datenverletzung hinweisen könnten.

Empfehlungen

  • Führen Sie Diagnoseskripte immer außerhalb der Hauptgeschäftszeiten aus, um mögliche Systembeeinträchtigungen zu vermeiden.
  • Vergewissern Sie sich, dass Sie die erforderlichen Genehmigungen eingeholt und die Beteiligten informiert haben, bevor Sie die Maßnahme durchführen.
  • Aktualisieren und pflegen Sie das Skript regelmäßig, um sich ändernden Systemstrukturen und -anforderungen gerecht zu werden.

Abschließende Überlegungen

Eine IT-Management-Plattform wie NinjaOne kann den Nutzen dieses Skripts erheblich steigern, indem sie IT-Fachleuten einen ganzheitlichen Überblick über den Systemzustand, die Leistung und Anomalien bietet. Die Integration solcher Skripte in NinjaOne bietet Einblicke in Echtzeit und macht die Speicherverwaltung zu einer mühelosen Aufgabe.

Nächste Schritte

Der Aufbau eines effizienten und effektiven IT-Teams erfordert eine zentralisierte Lösung, die als vereintes Tool für die Bereitstellung von Dienstleistungen fungiert. NinjaOne ermöglicht es IT-Teams, all ihre Geräte zu überwachen, verwalten, sichern und zu unterstützen, unabhängig von ihrem Ort und komplexer Infrastruktur vor Ort.

Erfahren Sie mehr über NinjaOne Endpoint Management schauen Sie sich eine Live-Tour an oder starten Sie Ihre kostenlose Testversion der NinjaOne Plattform.

Kategorien:

Das könnte Sie auch interessieren

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).