ChrisTitusTech / powershell-profile

Pretty PowerShell that looks good and functions almost as good as Linux terminal
551 stars 320 forks source link

Error when running setup script: "The '<' operator is reserved for future use." #3

Closed PMJeffery closed 1 year ago

PMJeffery commented 1 year ago

Running Windows 10, PowerShell 7.2.7 with or without "Run as Administrator" and in a writable directory (c:\Users\Username\Downloads)

When running the install command "irm "https://github.com/ChrisTitusTech/powershell-profile/blob/main/setup.ps1" | iex"

Output: "The '<' operator is reserved for future use."

Exits without doing anything

Tried in older Powershell 5.1.x and gets tons of errors, but similar to the above error message.

TheyCallMeZ commented 1 year ago

The correct install command is irm "https://github.com/ChrisTitusTech/powershell-profile/raw/main/setup.ps1" | iex

this goes to the raw script on github rather than the Blob page with all the html

PMJeffery commented 1 year ago

Good call @TheyCallMeZ

I tried that and now got this:

PS C:\Users\susername\Downloads> irm "https://github.com/ChrisTitusTech/powershell-profile/raw/main/setup.ps1" | iex
Exception:
Line |
   8 |           throw $_.Exception.Message
     |           ~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find a part of the path 'C:\Users\username\Documents\PowerShell\Microsoft.PowerShell_profile.ps1'.

I do not have that folder in my Documents folder. I added it, but created the folder and reran the command and it seems to work.

TheyCallMeZ commented 1 year ago

@PMJeffery That's interesting. The script successfully detected that you did not have an existing profile, but what's more interesting to me is that you didn't have the folder that would contain the profile. Either way you should be good to go from here.

SHJordan commented 1 year ago

Still getting the error message when powershell opens:

Set-Alias : Não é possível associar o argumento ao parâmetro 'Value' porque ele é nulo.
No C:\Users\iopdev\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:142 caractere:28
+ Set-Alias -Name vim -Value $EDITOR
+                            ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-Alias], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAliasCommand
TheyCallMeZ commented 1 year ago

@SHJordan You can fix this in one of two ways, if you want to have the alias of "vim" in your powershell you can edit the large if block to look something like this. Or you can Comment out the line setting the alias for vim.


    $EDITOR='nvim'
} elseif (Test-CommandExists pvim) {
    $EDITOR='pvim'
} elseif (Test-CommandExists vim) {
    $EDITOR='vim'
} elseif (Test-CommandExists vi) {
    $EDITOR='vi'
} elseif (Test-CommandExists code) {
    $EDITOR='code'
} elseif (Test-CommandExists notepad) {
    $EDITOR='notepad'
} elseif (Test-CommandExists notepad++) {
    $EDITOR='notepad++'
} elseif (Test-CommandExists sublime_text) {
    $EDITOR='sublime_text'
}
Set-Alias -Name vim -Value $EDITOR```
SHJordan commented 1 year ago

@SHJordan You can fix this in one of two ways, if you want to have the alias of "vim" in your powershell you can edit the large if block to look something like this. Or you can Comment out the line setting the alias for vim.

    $EDITOR='nvim'
} elseif (Test-CommandExists pvim) {
    $EDITOR='pvim'
} elseif (Test-CommandExists vim) {
    $EDITOR='vim'
} elseif (Test-CommandExists vi) {
    $EDITOR='vi'
} elseif (Test-CommandExists code) {
    $EDITOR='code'
} elseif (Test-CommandExists notepad) {
    $EDITOR='notepad'
} elseif (Test-CommandExists notepad++) {
    $EDITOR='notepad++'
} elseif (Test-CommandExists sublime_text) {
    $EDITOR='sublime_text'
}
Set-Alias -Name vim -Value $EDITOR```

that worked for me. thank you

SHJordan commented 1 year ago

Also, is there a way to make it work on powershell 7?

TheyCallMeZ commented 1 year ago

@SHJordan

You can run the setup on Powershell 7 but a number of the functions in Chris's script do not work such as uptime. If you just want the themed shell without all the extras Chris has baked in, I suggest installing oh my posh by following the installation guide on their website.
https://ohmyposh.dev/

SHJordan commented 1 year ago

@SHJordan

You can run the setup on Powershell 7 but a number of the functions in Chris's script do not work such as uptime. If you just want the themed shell without all the extras Chris has baked in, I suggest installing oh my posh by following the installation guide on their website. https://ohmyposh.dev/

i see... thats too bad... the looks are ok to get but i was more into the scripts.

TheyCallMeZ commented 1 year ago

@SHJordan Which parts of the script are not working for you in powershell 7?

You can replace the uptime function with either of these.

function uptime {
         # Needed to split the offset because of wmi reporting it in minutes instead of hours and minutes
        $bootUpTime = Get-WmiObject win32_operatingsystem | Select-Object lastbootuptime
        $plusMinus = $bootUpTime.lastbootuptime.SubString(21,1)
        $plusMinusMinutes = $bootUpTime.lastbootuptime.SubString(22, 3)
        $hourOffset = [int]$plusMinusMinutes/60
        $minuteOffset = 00
        if ($hourOffset -contains '.') { $minuteOffset = [int](60*[decimal]('.' + $hourOffset.ToString().Split('.')[1]))}       
        if ($hourOffset -lt 10 ) { $hourOffset = "0" + $hourOffset + $minuteOffset } else { $hourOffset = $hourOffset + $minuteOffset }
        $leftSplit = $bootUpTime.lastbootuptime.Split($plusMinus)[0]
        $upSince = [datetime]::ParseExact(($leftSplit + $plusMinus + $hourOffset), 'yyyyMMddHHmmss.ffffffzzz', $null)
        Get-WmiObject win32_operatingsystem | Select-Object @{LABEL='Machine Name'; EXPRESSION={$_.csname}}, @{LABEL='Last Boot Up Time'; EXPRESSION={$upsince}}
}
function uptime {
net statistics workstation | Select-String "since" | foreach-object {$_.ToString().Replace('Statistics since ', '')}
SHJordan commented 1 year ago

When i run the script on pwsh 7.2.7 i get:

PS C:\Windows\System32> irm "https://github.com/ChrisTitusTech/powershell-profile/raw/main/setup.ps1" | iex
Exception:
Line |
   8 |           throw $_.Exception.Message
     |           ~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find a part of the path 'C:\Users\iopdev\Documents\PowerShell\Microsoft.PowerShell_profile.ps1'.
TheyCallMeZ commented 1 year ago

@SHJordan do you have the Powershell folder in your Documents folder? I saw this on someone else's issue as well.

Alternatively you can open your profile and paste the whole or parts of the profile script in to gain those features.

SHJordan commented 1 year ago

I think i do, but i might be wrong?

PS C:\Windows\System32> $profile
C:\Users\iopdev\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
TheyCallMeZ commented 1 year ago

@SHJordan You need to actually check, $PROFILE is a system variable that reports where it should be, it doesn't actually check for existence.

I had to create a dummy account to get a clean slate and see if the folder is there by default and it is not.

image

Create that Powershell Folder in your Documents folder, run the setup after that and you should be good to go.

SHJordan commented 1 year ago

Indeed. I had to create the folder and fix the editor alias, other than that all seems good to go now. ty for your patience. image

SHJordan commented 1 year ago

@SHJordan Which parts of the script are not working for you in powershell 7?

You can replace the uptime function with either of these.

function uptime {
         # Needed to split the offset because of wmi reporting it in minutes instead of hours and minutes
        $bootUpTime = Get-WmiObject win32_operatingsystem | Select-Object lastbootuptime
        $plusMinus = $bootUpTime.lastbootuptime.SubString(21,1)
        $plusMinusMinutes = $bootUpTime.lastbootuptime.SubString(22, 3)
        $hourOffset = [int]$plusMinusMinutes/60
        $minuteOffset = 00
        if ($hourOffset -contains '.') { $minuteOffset = [int](60*[decimal]('.' + $hourOffset.ToString().Split('.')[1]))}       
        if ($hourOffset -lt 10 ) { $hourOffset = "0" + $hourOffset + $minuteOffset } else { $hourOffset = $hourOffset + $minuteOffset }
        $leftSplit = $bootUpTime.lastbootuptime.Split($plusMinus)[0]
        $upSince = [datetime]::ParseExact(($leftSplit + $plusMinus + $hourOffset), 'yyyyMMddHHmmss.ffffffzzz', $null)
        Get-WmiObject win32_operatingsystem | Select-Object @{LABEL='Machine Name'; EXPRESSION={$_.csname}}, @{LABEL='Last Boot Up Time'; EXPRESSION={$upsince}}
}
function uptime {
net statistics workstation | Select-String "since" | foreach-object {$_.ToString().Replace('Statistics since ', '')}

This actually isnt displaying the time, and the second version is actually broken.

TheyCallMeZ commented 1 year ago

@SHJordan

Corrected Both

function uptime {
         # Needed to split the offset because of wmi reporting it in minutes instead of hours and minutes
        $bootUpTime = Get-WmiObject win32_operatingsystem | Select-Object lastbootuptime
        $plusMinus = $bootUpTime.lastbootuptime.SubString(21,1)
        $plusMinusMinutes = $bootUpTime.lastbootuptime.SubString(22, 3)
        $hourOffset = [int]$plusMinusMinutes/60
        $minuteOffset = 00
        if ($hourOffset -contains '.') { $minuteOffset = [int](60*[decimal]('.' + $hourOffset.ToString().Split('.')[1]))}       
      if ([int]$hourOffset -lt 10 ) { $hourOffset = "0" + $hourOffset + $minuteOffset.ToString().PadLeft(2,'0') } else { $hourOffset = $hourOffset + $minuteOffset.ToString().PadLeft(2,'0') }
        $leftSplit = $bootUpTime.lastbootuptime.Split($plusMinus)[0]
        $upSince = [datetime]::ParseExact(($leftSplit + $plusMinus + $hourOffset), 'yyyyMMddHHmmss.ffffffzzz', $null)
        Get-WmiObject win32_operatingsystem | Select-Object @{LABEL='Machine Name'; EXPRESSION={$_.csname}}, @{LABEL='Last Boot Up Time'; EXPRESSION={$upsince}}
}
function uptime {
net statistics workstation | Select-String "since" | foreach-object {$_.ToString().Replace('Statistics since ', '')}
}
SHJordan commented 1 year ago

all good now ty

TheyCallMeZ commented 1 year ago

all good now ty

Glad to help!

ChrisTitusTech commented 1 year ago

Merged fix from @TheyCallMeZ commit dd1a411979d0a5f7290ac0d4a19c1b01078f3442