A PowerShell no le gusta | Un vistazo rápido a PowerShell
La introducción a PowerShell no me gusta
El siguiente artículo proporciona un resumen de lo que no le gusta a PowerShell. Los operadores de comparación permiten a los usuarios indicar condiciones para comparar valores y encontrar valores que satisfagan la condición mencionada. Para utilizar un operador de comparación, indique los valores que solo necesita comparar junto con un operador que aísle estos valores. El operador no me gusta es un tipo de operador de comparación coincidente. Esto, junto con los operadores Me gusta, Coincidencia y No coincidencia, son parte del tipo de operador de comparación coincidente. El operador no me gusta devuelve verdadero si la cadena no coincide con la condición especificada. Aquí veremos el operador no similar junto con otros operadores coincidentes.
Sintaxis del operador no me gusta:
A continuación se muestra la sintaxis del operador no similar:
<string[]> -notlike <wildcard-expression>Ejemplo:
Código:
"vigneshkrishnakumar" -notlike "*mar"
"vigneshkrishnakumar" -notlike "*sethu"Salida:
Trabajando como operador no como
El operador similar utiliza una expresión comodín para comparar. Por ejemplo, consideremos la frase “¿cuántos años tienes? Si ejecutamos el cmdlet "¿cuántos años tienes?", como "*tú", el cmdlet devolverá verdadero; sin embargo, si el cmdlet se modifica a "cuántos años tienes", como "tú", el cmdlet devolverá falso. Se debe al símbolo de asterisco que tienes delante. El símbolo de asterisco representa expresiones comodín.
Si el comodín está presente al principio del texto de comparación, significa que cualquier cosa puede ir antes de la expresión buscada, mientras que si el asterisco está presente al final, significa que la expresión buscada debe tener algo seguido. En el ejemplo anterior, si se coloca el asterisco después del texto, devolverá falso. El operador no similar es exactamente lo opuesto al operador similar. Devolverá verdadero cuando la condición sea falsa y falsa cuando haya una coincidencia.
Ejemplo:
Código:
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -like “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -like “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -like “is*”
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “is*”Salida:
Comparar me gusta y no me gusta con coincidir y no coincidir
Otro tipo de operador de comparación es el operador de coincidencia/no coincidencia. Aunque en comparación con Like, es mucho más PowerShell (aunque un poco más complicado). El administrador de operadores de coincidencia o no coincidencia emplea expresiones habituales (regex). Esto suele ser una gran ventaja y le da a Coordinate una ventaja inequívoca sobre Me gusta. En cualquier caso, si nunca ha utilizado expresiones habituales recientemente, planifíquelo usted mismo. Vayamos de nuevo con nuestra anterior cadena de ilustraciones.
Sintaxis para operador de coincidencia y no coincidencia:
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>Ejemplo #1:
Código:
"sun", "mon","tue","wed","thu","fri","sat" -match "su"
"viki", "vignesh","vikiii","vikii","nandhini","vyapini","sethu" -notmatch "viki"Salida:
Ejemplo #2:
Código:
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -match “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -match “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -match “is*”
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “is*”Salida:
Como puede ver en el resultado anterior, el operador de coincidencia no funciona con expresiones comodín.
Ejemplo #3:
Código:
$start=(Get-Date).Millisecond
Write-Host " wild card before the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -like “*there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -like “there”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -like “there*”
Write-Host "wild card before the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “*there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “there”
Write-Host "wild card after the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “there*”
$end=(Get-Date).Millisec
Write-Host "Total time in milli seconds for like and not like: $($start-$end)" -ForegroundColor Yellow
$start1=(Get-Date).Millisecond
Write-Host " wild card before the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -match “there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -match “now”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -match “america”
Write-Host "wild card before the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch “america”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch “sweden”
Write-Host "wild card after the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch "china”
$end1=(Get-Date).Millisec
Write-Host "Total time in milli seconds for match and not match: $($start1-$end1)" -ForegroundColor YellowSalida:
Como puede ver en el resultado anterior, me gusta y no me gusta tardaron menos en ejecutarse.
Conclusión: no le gusta PowerShell
Por lo tanto, en este artículo vimos en detalle el operador no me gusta. Aquí vimos la sintaxis, el uso y sus ventajas con ejemplos apropiados. Aquí también vimos los operadores de coincidencia y no coincidencia y sus similitudes y diferencias con el operador no similar.





