HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

Getting essential computer info for network admins

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
infoessentialgettingforcomputeradminsnetwork

Problem

I recently had to install VNC on mass across a lot of computers on a domain and needed some info about their PC, for example:

  • Host name



  • Username



  • MAC Address



  • IPv4 Address



  • Domain name



Once all of this info is gathered, it is compiled into a text file and uploaded to a FTP server. To save files from being conflicted with the same name, I set each file to be named the name of the user.

This is the first ever script and first experience I have had with Powershell so please tell me if there is anything I can improve.

```
function Get-MACAddress {
ipconfig /all | findstr "Physical" | Where-Object {$_.length -lt 58}
}
function Get-IPAddress {
ipconfig | findstr "IPv4 Address"
}
function Get-HostName {
get-content env:computername
}
function Get-UserName{
get-content env:UserName
}
function Get-DomainName{
get-content env:USERDOMAIN
}
function CreateCSVdocument {
Get-MACAddress | New-Item C:\Users\$($env:username)\Documents\$($env:username).txt -type file -force
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Get-IPAddress | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Hostname:"
Get-Hostname | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "User:"
Get-UserName | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Domain:"
Get-DomainName | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
}
function FTPUpload {
$Dir="C:\Users\$($env:username)\Documents\$($env:username).txt"

#ftp server
$ftp = "

Solution

It looks quite fine to me.

I'm mainly surprised by the massive duplication in the CreateCSVdocument function:

function CreateCSVdocument {
    Get-MACAddress | New-Item C:\Users\$($env:username)\Documents\$($env:username).txt -type file -force 
    Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Get-IPAddress | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Hostname:"
Get-Hostname | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "


It would be better to introduce a variable there and use that, for example:

function CreateCSVdocument {
    $Dir="C:\Users\$($env:username)\Documents\$($env:username).txt" 

    Get-MACAddress | New-Item $Dir -type file -force 

    Add-Content $Dir " "    
    Get-IPAddress | Add-Content $Dir
    Add-Content $Dir " "

    Add-Content $Dir "Hostname:"
    Get-Hostname | Add-Content $Dir
    Add-Content $Dir " "


Actually, this is still too repetitive.
Probably there is a better way to output all the text and pipe the whole thing to ... | Add-Content $Dir once at the end,
but I don't know how to do that in powershell.

The formatting is inconsistent:

-
In function definitions, sometimes you put a space between function name and { and sometimes you don't:

function Get-MACAddress {
function Get-UserName{


I recommend to put a space always, consistently.

-
Indentation is inconsistent:

  • You correctly indented the body of Get-MACAddress, Get-IPAddress and others



  • But you didn't follow the same logic in Get-HostName, Get-UserName, Get-DomainName and others

Code Snippets

function CreateCSVdocument {
    Get-MACAddress | New-Item C:\Users\$($env:username)\Documents\$($env:username).txt -type file -force 
    Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Get-IPAddress | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Hostname:"
Get-Hostname | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "
function CreateCSVdocument {
    $Dir="C:\Users\$($env:username)\Documents\$($env:username).txt" 

    Get-MACAddress | New-Item $Dir -type file -force 

    Add-Content $Dir " "    
    Get-IPAddress | Add-Content $Dir
    Add-Content $Dir " "

    Add-Content $Dir "Hostname:"
    Get-Hostname | Add-Content $Dir
    Add-Content $Dir " "
function Get-MACAddress {
function Get-UserName{

Context

StackExchange Code Review Q#70518, answer score: 3

Revisions (0)

No revisions yet.