@@ -1329,6 +1329,134 @@ public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState s
1329
1329
1330
1330
}
1331
1331
1332
+ /// <summary>
1333
+ /// Check if the function name starts with one of potentailly state changing verbs
1334
+ /// </summary>
1335
+ /// <param name="functionName"></param>
1336
+ /// <returns>true if the function name starts with a state changing verb, otherwise false</returns>
1337
+ public bool IsStateChangingFunctionName ( string functionName )
1338
+ {
1339
+ if ( functionName == null )
1340
+ {
1341
+ throw new ArgumentNullException ( "functionName" ) ;
1342
+ }
1343
+ // Array of verbs that can potentially change the state of a system
1344
+ string [ ] stateChangingVerbs =
1345
+ {
1346
+ "Restart-" ,
1347
+ "Stop-" ,
1348
+ "New-" ,
1349
+ "Set-" ,
1350
+ "Update-" ,
1351
+ "Reset-" ,
1352
+ "Remove-"
1353
+ } ;
1354
+ foreach ( var verb in stateChangingVerbs )
1355
+ {
1356
+ if ( functionName . StartsWith ( verb , StringComparison . OrdinalIgnoreCase ) )
1357
+ {
1358
+ return true ;
1359
+ }
1360
+ }
1361
+ return false ;
1362
+ }
1363
+
1364
+ /// <summary>
1365
+ /// Get the SupportShouldProcess attribute ast
1366
+ /// </summary>
1367
+ /// <param name="attributeAsts"></param>
1368
+ /// <returns>Returns SupportShouldProcess attribute ast if it exists, otherwise returns null</returns>
1369
+ public NamedAttributeArgumentAst GetShouldProcessAttributeAst ( IEnumerable < AttributeAst > attributeAsts )
1370
+ {
1371
+ if ( attributeAsts == null )
1372
+ {
1373
+ throw new ArgumentNullException ( "attributeAsts" ) ;
1374
+ }
1375
+ var cmdletBindingAttributeAst = this . GetCmdletBindingAttributeAst ( attributeAsts ) ;
1376
+ if ( cmdletBindingAttributeAst == null
1377
+ || cmdletBindingAttributeAst . NamedArguments == null )
1378
+ {
1379
+ return null ;
1380
+ }
1381
+ foreach ( var namedAttributeAst in cmdletBindingAttributeAst . NamedArguments )
1382
+ {
1383
+ if ( namedAttributeAst != null
1384
+ && namedAttributeAst . ArgumentName . Equals (
1385
+ "SupportsShouldProcess" ,
1386
+ StringComparison . OrdinalIgnoreCase ) )
1387
+ {
1388
+ return namedAttributeAst ;
1389
+ }
1390
+ }
1391
+ return null ;
1392
+ }
1393
+
1394
+ /// <summary>
1395
+ /// Get the CmdletBinding attribute ast
1396
+ /// </summary>
1397
+ /// <param name="attributeAsts"></param>
1398
+ /// <returns>Returns CmdletBinding attribute ast if it exists, otherwise returns null</returns>
1399
+ public AttributeAst GetCmdletBindingAttributeAst ( IEnumerable < AttributeAst > attributeAsts )
1400
+ {
1401
+ if ( attributeAsts == null )
1402
+ {
1403
+ throw new ArgumentNullException ( "attributeAsts" ) ;
1404
+ }
1405
+ foreach ( var attributeAst in attributeAsts )
1406
+ {
1407
+ if ( attributeAst == null || attributeAst . NamedArguments == null )
1408
+ {
1409
+ continue ;
1410
+ }
1411
+ if ( attributeAst . TypeName . GetReflectionAttributeType ( )
1412
+ == typeof ( CmdletBindingAttribute ) )
1413
+ {
1414
+ return attributeAst ;
1415
+ }
1416
+ }
1417
+ return null ;
1418
+ }
1419
+
1420
+ /// <summary>
1421
+ /// Get the boolean value of the named attribute argument
1422
+ /// </summary>
1423
+ /// <param name="namedAttributeArgumentAst"></param>
1424
+ /// <returns>Boolean value of the named attribute argument</returns>
1425
+ public bool GetNamedArgumentAttributeValue ( NamedAttributeArgumentAst namedAttributeArgumentAst )
1426
+ {
1427
+ if ( namedAttributeArgumentAst == null )
1428
+ {
1429
+ throw new ArgumentNullException ( "namedAttributeArgumentAst" ) ;
1430
+ }
1431
+ if ( namedAttributeArgumentAst . ExpressionOmitted )
1432
+ {
1433
+ return true ;
1434
+ }
1435
+ else
1436
+ {
1437
+ var varExpAst = namedAttributeArgumentAst . Argument as VariableExpressionAst ;
1438
+ if ( varExpAst == null )
1439
+ {
1440
+ var constExpAst = namedAttributeArgumentAst . Argument as ConstantExpressionAst ;
1441
+ if ( constExpAst == null )
1442
+ {
1443
+ return false ;
1444
+ }
1445
+ bool constExpVal ;
1446
+ if ( LanguagePrimitives . TryConvertTo < bool > ( constExpAst . Value , out constExpVal ) )
1447
+ {
1448
+ return constExpVal ;
1449
+ }
1450
+ }
1451
+ else if ( varExpAst . VariablePath . UserPath . Equals (
1452
+ bool . TrueString ,
1453
+ StringComparison . OrdinalIgnoreCase ) )
1454
+ {
1455
+ return true ;
1456
+ }
1457
+ }
1458
+ return false ;
1459
+ }
1332
1460
1333
1461
#endregion
1334
1462
}
0 commit comments